在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:koajs/joi-router开源软件地址:https://github.com/koajs/joi-router开源编程语言:JavaScript 100.0%开源软件介绍:joi-routerEasy, rich and fully validated koa routing. Features:
Node compatibilityNodeJS Exampleconst koa = require('koa');
const router = require('koa-joi-router');
const Joi = router.Joi;
const public = router();
public.get('/', async (ctx) => {
ctx.body = 'hello joi-router!';
});
public.route({
method: 'post',
path: '/signup',
validate: {
body: {
name: Joi.string().max(100),
email: Joi.string().lowercase().email(),
password: Joi.string().max(100),
_csrf: Joi.string().token()
},
type: 'form',
output: {
200: {
body: {
userId: Joi.string(),
name: Joi.string()
}
}
}
},
handler: async (ctx) => {
const user = await createUser(ctx.request.body);
ctx.status = 201;
ctx.body = user;
}
});
const app = new koa();
app.use(public.middleware());
app.listen(3000); Usage
const Koa = require("koa")
const router = require('koa-joi-router');
const pub = router();
const admin = router();
const auth = router();
// add some routes ..
pub.get('/some/path', async () => {});
admin.get('/admin', async () => {});
auth.post('/auth', async () => {});
const app = new Koa();
app.use(pub.middleware());
app.use(admin.middleware());
app.use(auth.middleware());
app.listen(); Module properties.JoiIt is HIGHLY RECOMMENDED you use this bundled version of Joi to avoid bugs related to passing an object created with a different release of Joi into the router. const koa = require('koa');
const router = require('koa-joi-router');
const Joi = router.Joi; Router instance methods.route()Adds a new route to the router. const router = require('koa-joi-router');
const public = router();
public.route({
method: 'post',
path: '/signup',
validate: {
header: joiObject,
query: joiObject,
params: joiObject,
body: joiObject,
maxBody: '64kb',
output: { '400-600': { body: joiObject } },
type: 'form',
failure: 400,
continueOnError: false
},
pre: async (ctx, next) => {
await checkAuth(ctx);
return next();
},
handler: async (ctx) => {
await createUser(ctx.request.body);
ctx.status = 201;
},
meta: { 'this': { is: 'stored internally with the route definition' }}
}); or const router = require('koa-joi-router');
const public = router();
const routes = [
{
method: 'post',
path: '/users',
handler: async (ctx) => {}
},
{
method: 'get',
path: '/users',
handler: async (ctx) => {}
}
];
public.route(routes); .route() options
.get(),post(),put(),delete() etc - HTTP methods
const router = require('koa-joi-router');
const admin = router();
// signature: router.method(path [, config], handler [, handler])
admin.put('/thing', handler);
admin.get('/thing', middleware, handler);
admin.post('/thing', config, handler);
admin.delete('/thing', config, middleware, handler); .use()Middleware run in the order they are defined by .use()(or .get(), etc.) They are invoked sequentially, requests start at the first middleware and work their way "down" the middleware stack which matches Express 4 API. const router = require('koa-joi-router');
const users = router();
users.get('/:id', handler);
users.use('/:id', runThisAfterHandler); .prefix()Defines a route prefix for all defined routes. This is handy in "mounting" scenarios. const router = require('koa-joi-router');
const users = router();
users.get('/:id', handler);
// GET /users/3 -> 404
// GET /3 -> 200
users.prefix('/users');
// GET /users/3 -> 200
// GET /3 -> 404 .param()Defines middleware for named route parameters. Useful for auto-loading or validation. See @koa/router const router = require('koa-joi-router');
const users = router();
const findUser = (id) => {
// stub
return Promise.resolve('Cheddar');
};
users.param('user', async (id, ctx, next) => {
const user = await findUser(id);
if (!user) return ctx.status = 404;
ctx.user = user;
await next();
});
users.get('/users/:user', (ctx) => {
ctx.body = `Hello ${ctx.user}`;
});
// GET /users/3 -> 'Hello Cheddar' .middleware()Generates routing middleware to be used with const router = require('koa-joi-router');
const public = router();
public.get('/home', homepage);
const app = koa();
app.use(public.middleware()); // wired up
app.listen(); Additions to ctx.stateThe route definition for the currently matched route is available
via const router = require('koa-joi-router');
const public = router();
public.get('/hello', async (ctx) => {
console.log(ctx.state.route);
}); Additions to ctx.requestWhen using the ctx.request.bodyThe
jsonWhen admin.route({
method: 'post',
path: '/blog',
validate: { type: 'json' },
handler: async (ctx) => {
console.log(ctx.request.body); // the incoming json as an object
}
}); formWhen admin.route({
method: 'post',
path: '/blog',
validate: { type: 'form' },
handler: async (ctx) => {
console.log(ctx.request.body) // the incoming form as an object
}
}); ctx.request.partsThe
multipartWhen admin.route({
method: 'post',
path: '/blog',
validate: { type: 'multipart' },
handler: async (ctx) => {
const parts = ctx.request.parts;
let part;
try {
while ((part = await parts)) {
// do something with the incoming part stream
part.pipe(someOtherStream);
}
} catch (err) {
// handle the error
}
console.log(parts.field.name); // form data
}
}); Handling non-validated inputNote: if you do not specify a value for admin.route({
method: 'post',
path: '/blog',
validate: { },
handler: async (ctx) => {
console.log(ctx.request.body, ctx.request.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论