express get variable from url code example

Example 1: express get url parameters

app.get('/path/:name', function(req, res) {
  res.send("tagId is set to " + req.params.name);
});

Example 2: express param in url

app.get('/p/:tagId', function(req, res) {
  res.send("tagId is set to " + req.params.tagId);
});

// GET /p/5
// tagId is set to 5

Example 3: express route parameters

Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

Example 4: route parameter in node

var express = require('express');
var fs  = require('fs');
var app= express();
 app.get('/index/profile/:id',function(req,res){
    //  res.send('profile with id' + req.params.id)

    
    
 });
app.listen(3000,'127.0.0.1');
console.log('lsitng');

Example 5: express route parameters

Route path: /flights/:from-:to
Request URL: http://localhost:3000/flights/LAX-SFO
req.params: { "from": "LAX", "to": "SFO" }