Wednesday 6 March 2019

Express: req.route: Get currently matched route


req.route
It represents the currently matched route.

index.js
const express = require('express')
var cookieParser = require('cookie-parser')

const app = express()

app.use(cookieParser()); 

app.get('/', (req, res) => res.send(`Request protocol ${req.protocol}`))

app.get('/user', (req, res) => {
 console.log(req.route)
 res.send("Hello user, Welcome!!!!!!")
})


const port = 3000
app.listen(port, () => console.log(`Application started listening on port ${port}!`));

Run index.js

Open the url ‘http://localhost:3000/user’, in browser, you will see below messages in console.

Route {
  path: '/user',
  stack:
   [ Layer {
       handle: [Function],
       name: '<anonymous>',
       params: undefined,
       path: undefined,
       keys: [],
       regexp: /^\/?$/i,
       method: 'get' } ],

  methods: { get: true } }

Previous                                                 Next                                                 Home

No comments:

Post a Comment