Skip to content

erin-noe-payne/super-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Super Router

Travis build status

API Docs

Code coverage Report

const SuperRouter = require('super-router');
const app      = new SuperRouter.App();
const router   = new SuperRouter.Router();

router.addRoute({
  path    : '/cases',
  methods : 'get',
  handler : (opts) => {
    const request  = opts.request;
    const response = opts.response;

    return Database.getCases().then((cases)=>{
      response.setBody(cases);
    });
  }
});

router.addRoute({
  path    : '/cases/:caseId',
  methods : 'get',
  handler : (opts) => {
    const request  = opts.request;
    const response = opts.response;

    return Database.getCaseById(request.routeParams.caseId).then((case)=>{
      response.setBody(case);
    });
  }
});

app.then({
  path : '/cases/externalId/:externalId(/*restOfRoute)',
  handler : (opts) => {
    const request  = opts.request;
    const response = opts.response;

    return Database.getCaseByExternalId(request.routeParams.externalId).then((case)=>{
      request.path = `/cases/${case.id}${request.routeParams.restOfRoute}`
    });
  }
});
app.then(router.match);
app.then(router.execute);
app.then((opts) => {
  const request  = opts.request;
  const response = opts.response;

  response.setBody(JSON.stringify(response.getBody()));
})