owebjs 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +224 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,13 +1,233 @@
|
|
|
1
1
|
# Oweb
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A flexible and modern web framework built on top of Fastify, designed for creating scalable and maintainable web applications with file-based routing and hot module replacement.
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<img src="https://img.shields.io/npm/v/owebjs" alt="npm version">
|
|
7
|
+
<img src="https://img.shields.io/npm/l/owebjs" alt="license">
|
|
8
|
+
<img src="https://img.shields.io/npm/dt/owebjs" alt="downloads">
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **File-based Routing**: Automatically generate routes based on your file structure
|
|
14
|
+
- **Hot Module Replacement (HMR)**: Update your routes without restarting the server
|
|
15
|
+
- **Middleware Support**: Use hooks to add middleware functionality
|
|
16
|
+
- **Error Handling**: Global and route-specific error handling
|
|
17
|
+
- **TypeScript Support**: Built with TypeScript for better developer experience
|
|
18
|
+
- **Plugin System**: Extend functionality with plugins
|
|
19
|
+
- **uWebSockets.js Support**: Optional high-performance WebSocket server
|
|
4
20
|
|
|
5
21
|
## Installation
|
|
6
22
|
|
|
23
|
+
```bash
|
|
24
|
+
npm install owebjs
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
import Oweb from 'owebjs';
|
|
31
|
+
|
|
32
|
+
// Create and setup the app
|
|
33
|
+
const app = await new Oweb().setup();
|
|
34
|
+
|
|
35
|
+
// Load routes from a directory
|
|
36
|
+
await app.loadRoutes({
|
|
37
|
+
directory: 'routes',
|
|
38
|
+
hmr: {
|
|
39
|
+
enabled: true, // Enable hot module replacement
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Start the server
|
|
44
|
+
await app.start({ port: 3000 });
|
|
45
|
+
console.log('Server running at http://localhost:3000');
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Creating Routes
|
|
49
|
+
|
|
50
|
+
Routes are automatically generated based on your file structure. Create a file in your routes directory:
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
// routes/hello.js
|
|
54
|
+
import { Route } from 'owebjs';
|
|
55
|
+
|
|
56
|
+
export default class extends Route {
|
|
57
|
+
async handle(req, res) {
|
|
58
|
+
res.send({ message: 'Hello, World!' });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This will create a GET route at `/hello`.
|
|
64
|
+
|
|
65
|
+
### Dynamic Routes
|
|
66
|
+
|
|
67
|
+
Use brackets to create dynamic route parameters:
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
// routes/users/[id].js
|
|
71
|
+
import { Route } from 'owebjs';
|
|
72
|
+
|
|
73
|
+
export default class extends Route {
|
|
74
|
+
async handle(req, res) {
|
|
75
|
+
res.send({ userId: req.params.id });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This will create a GET route at `/users/:id`.
|
|
81
|
+
|
|
82
|
+
### HTTP Methods
|
|
83
|
+
|
|
84
|
+
Specify the HTTP method in the filename:
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
// routes/api/users.post.js
|
|
88
|
+
import { Route } from 'owebjs';
|
|
89
|
+
|
|
90
|
+
export default class extends Route {
|
|
91
|
+
async handle(req, res) {
|
|
92
|
+
// Create a new user
|
|
93
|
+
const user = req.body;
|
|
94
|
+
res.status(201).send({ id: 1, ...user });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Middleware (Hooks)
|
|
100
|
+
|
|
101
|
+
Create hooks to add middleware functionality:
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
// routes/_hooks.js
|
|
105
|
+
import { Hook } from 'owebjs';
|
|
106
|
+
|
|
107
|
+
export default class extends Hook {
|
|
108
|
+
handle(req, res, done) {
|
|
109
|
+
console.log(`${req.method} ${req.url}`);
|
|
110
|
+
done(); // Continue to the next hook or route handler
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Hooks are applied to all routes in the current directory and its subdirectories.
|
|
116
|
+
|
|
117
|
+
## Error Handling
|
|
118
|
+
|
|
119
|
+
### Global Error Handler
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
app.setInternalErrorHandler((req, res, error) => {
|
|
123
|
+
console.error(error);
|
|
124
|
+
res.status(500).send({
|
|
125
|
+
error: 'Internal Server Error',
|
|
126
|
+
message: error.message,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Route-specific Error Handler
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
import { Route } from 'owebjs';
|
|
135
|
+
|
|
136
|
+
export default class extends Route {
|
|
137
|
+
async handle(req, res) {
|
|
138
|
+
throw new Error('Something went wrong');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
handleError(req, res, error) {
|
|
142
|
+
res.status(500).send({
|
|
143
|
+
error: 'Route Error',
|
|
144
|
+
message: error.message,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Plugins
|
|
151
|
+
|
|
152
|
+
Oweb supports Fastify plugins and comes with some built-in plugins:
|
|
153
|
+
|
|
154
|
+
### Using Fastify Plugins
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
import Oweb from 'owebjs';
|
|
158
|
+
import fastifyMultipart from '@fastify/multipart';
|
|
159
|
+
|
|
160
|
+
const app = await new Oweb().setup();
|
|
161
|
+
|
|
162
|
+
// Register Fastify plugin
|
|
163
|
+
await app.register(fastifyMultipart, {
|
|
164
|
+
limits: {
|
|
165
|
+
fileSize: 10 * 1024 * 1024, // 10MB
|
|
166
|
+
},
|
|
167
|
+
});
|
|
7
168
|
```
|
|
8
|
-
|
|
169
|
+
|
|
170
|
+
### Using Built-in Plugins
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
import { Route } from 'owebjs';
|
|
174
|
+
import { ChunkUpload } from 'owebjs/dist/plugins';
|
|
175
|
+
|
|
176
|
+
export default class extends Route {
|
|
177
|
+
async handle(req, res) {
|
|
178
|
+
const file = await req.file();
|
|
179
|
+
const buffer = await file.toBuffer();
|
|
180
|
+
|
|
181
|
+
await ChunkUpload(
|
|
182
|
+
{
|
|
183
|
+
buffer,
|
|
184
|
+
fileName: file.filename,
|
|
185
|
+
currentChunk: +req.query.currentChunk,
|
|
186
|
+
totalChunks: +req.query.totalChunks,
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
path: './uploads',
|
|
190
|
+
maxChunkSize: 5 * 1024 * 1024, // 5MB
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
return res.status(204).send();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
9
197
|
```
|
|
10
198
|
|
|
11
|
-
##
|
|
199
|
+
## Advanced Configuration
|
|
12
200
|
|
|
13
|
-
|
|
201
|
+
### uWebSockets.js Support
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
const app = await new Oweb({ uWebSocketsEnabled: true }).setup();
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Custom Route Options
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
import { Route } from 'owebjs';
|
|
211
|
+
|
|
212
|
+
export default class extends Route {
|
|
213
|
+
constructor() {
|
|
214
|
+
super({
|
|
215
|
+
schema: {
|
|
216
|
+
body: {
|
|
217
|
+
type: 'object',
|
|
218
|
+
required: ['username', 'password'],
|
|
219
|
+
properties: {
|
|
220
|
+
username: { type: 'string' },
|
|
221
|
+
password: { type: 'string' },
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async handle(req, res) {
|
|
229
|
+
// Body is validated according to the schema
|
|
230
|
+
res.send({ success: true });
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "owebjs",
|
|
3
|
-
"version": "1.3.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "A flexible and modern web framework built on top of Fastify",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"exports": {
|