@jnode/server 2.0.0 → 2.0.2
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 +8 -0
- package/package.json +1 -1
- package/src/handlers.js +1 -1
- package/src/routers.js +16 -2
package/README.md
CHANGED
|
@@ -346,6 +346,14 @@ Collects a path segment and saves it to `ctx.params[paramName]`, then advances t
|
|
|
346
346
|
|
|
347
347
|
Collects a host segment and saves it to `ctx.params[paramName]`, then advances the host pointer and continues routing.
|
|
348
348
|
|
|
349
|
+
### Router: `SetCodeRouter(codeHandlers, next)`
|
|
350
|
+
|
|
351
|
+
- `codeHandlers` [\<Object\>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
|
|
352
|
+
- `<STATUS_CODE>` [handler-extended](#handler-extended) A handler to respond when the specified HTTP status code is thrown. E.G., `404: h.Text('Not found!', { statusCode: 404 })`, `500: h.JSON({ error: 'Internal server error' }, { statusCode: 500 })`.
|
|
353
|
+
- `next` [router](#class-serverrouter) | [handler-extended](#handler-extended) The next router or handler to call after setting up the code handlers.
|
|
354
|
+
|
|
355
|
+
Sets custom handlers for specific HTTP status codes, which will be used when handlers throw numeric error codes. The code handlers set by this router are merged with existing ones in `env.codeHandlers`.
|
|
356
|
+
|
|
349
357
|
## Built-in handlers
|
|
350
358
|
|
|
351
359
|
We provide two methods to use them:
|
package/package.json
CHANGED
package/src/handlers.js
CHANGED
|
@@ -54,7 +54,7 @@ class DataHandler {
|
|
|
54
54
|
// file handler: local file
|
|
55
55
|
class FileHandler {
|
|
56
56
|
constructor(file, options = {}) {
|
|
57
|
-
this.file = path.resolve(
|
|
57
|
+
this.file = path.resolve(file);
|
|
58
58
|
this.options = options;
|
|
59
59
|
|
|
60
60
|
// range may be disabled by `options.disableRange` or when `statusCode` is set to non-200 value
|
package/src/routers.js
CHANGED
|
@@ -196,15 +196,29 @@ class HostArgRouter {
|
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
+
// set code router: set the code handler for specific status code
|
|
200
|
+
class SetCodeRouter {
|
|
201
|
+
constructor(codeHandlers, next) {
|
|
202
|
+
this.codeHandlers = codeHandlers;
|
|
203
|
+
this.next = next;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
route(env, ctx) {
|
|
207
|
+
env.codeHandlers = Object.assign({}, env.codeHandlers, this.codeHandlers);
|
|
208
|
+
return this.next;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
199
212
|
// export
|
|
200
213
|
module.exports = {
|
|
201
|
-
PathRouter, HostRouter, MethodRouter, FunctionRouter, PathArgRouter, HostArgRouter,
|
|
214
|
+
PathRouter, HostRouter, MethodRouter, FunctionRouter, PathArgRouter, HostArgRouter, SetCodeRouter,
|
|
202
215
|
routerConstructors: {
|
|
203
216
|
Path: (...args) => new PathRouter(...args),
|
|
204
217
|
Host: (...args) => new HostRouter(...args),
|
|
205
218
|
Method: (...args) => new MethodRouter(...args),
|
|
206
219
|
Function: (...args) => new FunctionRouter(...args),
|
|
207
220
|
PathArg: (...args) => new PathArgRouter(...args),
|
|
208
|
-
HostArg: (...args) => new HostArgRouter(...args)
|
|
221
|
+
HostArg: (...args) => new HostArgRouter(...args),
|
|
222
|
+
SetCode: (...args) => new SetCodeRouter(...args)
|
|
209
223
|
}
|
|
210
224
|
};
|