Get Query String variable in Expressjs
In this blog, we’ll learn how to handle route parameters in an Express.js application.
Handling Route Parameters in Express js
when making a server side application with the help of node js,Express.js is one of most popular framework to work with.Ot having one useful feature that in express handling route parameter allow your application to capture and process dynamic data from the URL.
Setting Express Application
Firstly you have to install express from node package manager.
npm install express
Now, let’s create a basic Express server.
const express = require('express'); const app = express(); app.use(express.json()); app.listen(8000, () => { console.log("Listening on port 8000"); });
With this above mentioned code our server now will start working and it is listening on port 8000.Now next will make route parameter.
Handling Route Parameter
Now will create the Route that will capture the id parameter from the URL
app.get('/home/:id', (req, res) => { let { id } = req.params; res.send(id); });
whole code is given below
const express=require('express'); const app=express() app.use(express.json()) app.listen(8000,()=>{ console.log("Listening on port 8000"); }) app.get('/home/:id',(req,res)=>{ let id=req.params; res.send(id) })
Output :
Leave a Reply