YEPS Router
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
tests
.editorconfig
.eslintrc
.gitignore
.istanbul.yml
.travis.yml
LICENSE
README.md
appveyor.yml
index.js
package.json

README.md

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