trafico

1.0.8 • Public • Published

Tráfico

npm version Downloads Build Status Coverage Status codebeat badge Greenkeeper badge

🚥 Awesome -zero dependency- router for Express.

Tráfico will map routes to controllers and enable them in your Express application, so you don't have to do it manually and for each one. This provides an easier abstraction and enables a drop-in-and-use route/controller setup.

Basic use

const express = require('express');
const Trafico = require('trafico');
 
const app = express();
const trafico = new Trafico({
  express,
  routes: `/path/to/routes`,
  controllers: `/path/to/controllers`
});
 
app.use(trafico.route());
app.listen(port, () => {
  console.log(`Up on port: ${port}`);
});

Routes

In your routes folder (/path/to/routes) create the routes you need to be mapped to your application. For example:

| path/to/routes
  | home.js
  | user.js

The home.js route would look similar to this (define your routes as you normally would in your Express application):

module.exports = (router, controller) => {
  router.get('/', controller.index);
  router.get('/date', controller.date);
 
  return router;
};

Controllers

Tráfico will load all routes from the routes path you specify and try to look for the controllers to match them. Create your controllers in the controllers folder (/path/to/controllers). Controllers must be named like their corresponding routes.

| path/to/controllers
  | home.js
  | user.js

The home.js controller would expose the methods mapped in the route:

module.exports = {
  index: (req, res) => {
    res.send({ hello: 'world' });
  },
  
  date: (req, res) => {
    res.send({ date: +new Date() });
  }
};

Working examples

Have a look at the /test folder. ExpressBoilerplate also uses Tráfico.

Contribute

fork https://github.com/aichholzer/trafico

License

MIT

Package Sidebar

Install

npm i trafico

Weekly Downloads

38

Version

1.0.8

License

MIT

Unpacked Size

10.4 kB

Total Files

5

Last publish

Collaborators

  • saichholzer