yeps-router
v1.2.0
Published
YEPS promise based router
Downloads
36
Maintainers
Readme
YEPS Router
YEPS promise based router
How to install
npm i -S yeps-router
How to use
const App = require('yeps');
const Router = require('yeps-router');
const error = require('yeps-error');
const logger = require('yeps-logger');
const server = require('yeps-server');
const bodyParser = require('yeps-bodyparser');
const methodOverride = require('yeps-method-override');
const app = new App();
app.all([
error(),
logger(),
bodyParser(),
methodOverride()
]);
const router = new Router();
router.get('/').then(async (ctx) => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
});
app.then(router.resolve());
server.createHttpServer(app);
Methods
- all(url)
- head(url)
- options(url)
- get(url)
- post(url)
- patch(url)
- post(url)
- delete(url) or del(url)
All methods are wrappers for catch() method:
catch({ method: 'GET', url: '/' })
router.catch().then(async (ctx) => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
});
router.catch({ method: 'POST' }).then(async (ctx) => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
});
router.catch({ url: '/data' }).then(async (ctx) => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
});
router.catch({ method: 'POST', url: '/data' }).then(async (ctx) => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
});
Chain
You can use chain of methods:
router.get('/').then(async (ctx) => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
}).post('/data').then(async (ctx) => {
ctx.res.statusCode = 200;
ctx.res.end('homepage');
});
Request parameters
Query
url: /?data=test
router.get('/').then(async (ctx) => {
ctx.request.query.data === 'test'
});
Parameters
url: /test/125
router.get('/test/:id').then(async (ctx) => {
ctx.request.params.id === '125'
});