1.2.0 • Published 7 years ago

yeps-router v1.2.0

Weekly downloads
20
License
MIT
Repository
github
Last release
7 years ago

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

1.2.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago