Monday, 4 March 2019

Express: app.route(path): Return the router instance for this path


app.route(path)
Return the router instance for this path. You can use this instance to handle HTTP verbs with optional middleware. Use app.route() to avoid duplicate route names

index.js
var express = require('express')

var app = express()

app.get('/', (req, res) => {
 res.send('Hello World')
})

app.route('/posts')
 .all(function(req, res, next) {
  console.log("Request received")
  if(req.method === "GET" || req.method === 'POST')
   next()
  else
   res.send('Methods other than get, post are not supported')
 })
 .get(function(req, res, next) {
  console.log('Get request received')
  res.send('Get Request')
 })
 .post(function(req, res, next) {
  console.log('POST request received')
  res.send('POST request')
 });


var listener = app.listen(3000, () => {console.log('Application started on port ' + listener.address().port);});



Previous                                                 Next                                                 Home

No comments:

Post a Comment