yeps-router

1.2.0 • Public • Published

YEPS Router

YEPS promise based router

NPM

npm version Build Status Coverage Status Linux Build Windows Build

Dependency Status devDependency Status NSP Status

License GitHub stars GitHub forks GitHub issues Twitter

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'
});

YEPS documentation

Package Sidebar

Install

npm i yeps-router

Weekly Downloads

3

Version

1.2.0

License

MIT

Last publish

Collaborators

  • evheniy.bystrov