Socket
Socket
Sign inDemoInstall

express-routes-mapper

Package Overview
Dependencies
115
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    express-routes-mapper

a small mapper for express routes


Version published
Weekly downloads
479
increased by23.14%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

express-routes-mapper

Build Status Coverage Status

a simple package to map your routes for your expressjs application

getting started

start from ground

This is a example for a simple rest API.

1.) npm install

$ npm i -S express-routes-mapper

2.) mapped routes

Create your routes file:

// es6
const routes = {

  'POST /user': 'UserController.create'

}

export default routes;

//es5
module.exports = {

  'POST /user': 'UserController.create'

}

Every post request to your server to route '/user' will call the function 'create' on the 'UserController'.

3.) the controller

Create a file named UserController.js

//es6
export default class UserController {

  create (req, res) {

    res.send('created a User with es6');

  }

}

//es5
module.exports = {

  'create': function (req, res) {

    res.send('created a User with es5');

  }

}

4.) tell express.js app to use our routes

I assume you have a folder structure like this, but it can be adapted to any folder structure.

If you have a different, folder structure, and want to link to a different path look here.

.
+-- src
|   +-- config
|   |   +-- routes.js
|   |
|   +-- controllers
|   |   +-- UserController.js
|   |
|   +-- models
|   |
|   app.js
|
package.json

Your app.js could look a bit like this:

The magic happens here:

  • import routes from './config/routes'; the file where all the routes are mapped
  • import mapRoutes from 'express-routes-mapper'; the package that makes the mapping possible
  • app.use('/', mapRoutes(routes)); tell express to use the mapped routes and here
  • var routes = require('./config/routes'); the file where all the routes are mapped
  • var mapRoutes = require('express-routes-mapper'); the package that makes the mapping possible
  • app.use('/', mapRoutes(routes)); tell express to use the mapped routes
//es6
import express from 'express';
import http from 'http';

import routes from './config/routes';
import mapRoutes from 'express-routes-mapper';

const app = express();
const server = http.Server(app);
const port = 3338;

app.use('/', mapRoutes(routes));

server.listen(port, function() {
  console.log('There we go ♕');
  console.log(`Gladly listening on http://127.0.0.1:${port}`);
});

//es5
var express = require('express');
var http = require('http');

var routes = require('./config/routes');
var mapRoutes = require('express-routes-mapper');

var app = express();
var server = http.Server(app);
var port = 3339;

app.use('/', mapRoutes(routes));

server.listen(port, function(){
  console.log('There we go ♕');
  console.log('Gladly listening on http://127.0.0.1:' + port);
});

Supported methods

  • GET
  • POST
  • PUT
  • DELETE
{

  'GET    /someroute' : 'SomeController.somefunction',
  'POST   /someroute' : 'SomeController.somefunction',
  'PUT    /someroute' : 'SomeController.somefunction',
  'DELETE /someroute' : 'SomeController.somefunction'

}

Dynamic routes

Simply use a colon ':' for defining dynamic routes.

{
  'GET /someroute/:id' : 'SomeController.somefunction'
}

If you make a get request to http://localhost/someroute/1 the 1 (:id) is now in the 'SomeController accessible.

//es6
export default class SomeController {

  somefunction (req, res) {

    let id = req.params.id;

  }

}

//es5
module.exports = {

  'somefunction': function (req, res) {

    var id = req.params.id

  }

}

set path to controller

The only differnce is that you pass in the path to your file in the mapRoutes function.

  • app.use('/', mapRoutes(routes, '../../../path/to/new/file/'));
//es6
import express from 'express';
import http from 'http';

import routes from './config/routes';
import mapRoutes from 'express-routes-mapper';

const app = express();
const server = http.Server(app);
const port = 3338;

app.use('/', mapRoutes(routes, '../../../path/to/new/file/'));

server.listen(port, function() {
  console.log('There we go ♕');
  console.log(`Gladly listening on http://127.0.0.1:${port}`);
});

//es5
var express = require('express');
var http = require('http');

var routes = require('./config/routes');
var mapRoutes = require('express-routes-mapper');

var app = express();
var server = http.Server(app);
var port = 3339;

app.use('/', mapRoutes(routes, '../../../path/to/new/file/'));

server.listen(port, function(){
  console.log('There we go ♕');
  console.log('Gladly listening on http://127.0.0.1:' + port);
});

Keywords

FAQs

Last updated on 13 Mar 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc