Explain Routing in Express

Other topics

Express Router

Express router allows you to create multiple "mini apps" so you can namespace your api, public, auth and other routes into separate routing systems.

var express   = require('express');
var app       = express();
var router    = express.Router();

router.get('/', function(req, res){
   res.send('Get request received');
});

router.post('/', function(req, res){
   res.send('Post requestreceived');
});

app.use('/', router);

app.listen(8080);

Chainable route handlers for a route path by using app.route

var express   = require('express');
var app       = express();
var router    = express.Router();

app.route('/user')
  .get(function (req, res) {
    res.send('Get a random user')
  })
  .post(function (req, res) {
    res.send('Add a user')
  })
  .put(function (req, res) {
    res.send('Update the user details')
  })
  .delete(function (req, res) {
    res.send('Delete a user')
  });

Contributors

Topic Id: 6536

Example Ids: 22387,25624

This site is not affiliated with any of the contributors.