How to Get URL Parameters in Node.js?
In node.Js application, You can use the app.post() method to accept a POST request. req object is used to Get the post data inside the callback function of the app.post() method.
File Name :
const bodyContent = req.body;
// To access the header content
const headerContent = req.headers;
Steps : create Node Project
File Name :
Step 1:- First install the Node.Js in your system.
Step 2:- Create a project Directory. eg. mynode_project
Step 3:- C:\mynode_project>
Step 4:- Now install express Js inside your project
Step 5:- c:\mynode_project> npm install express
Step 6 :- create file myapp.js in your project directory.
Step 7 :- create file index.html in your project directory.
Step 8 :- run your project. C:\mynode_project> node myapp.js
Myapp.js
File Name : myapp.js
const express = require('express');
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/', (req, res) => {
const { username, password } = req.body;
const { authorization } = req.headers;
res.send({
username,
password,
authorization,
});
});
app.listen(3000);
// app.listen(8080);
View Page index.html
File Name : index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>How to get Post data in node.Js</title>
</head>
<body>
<form>
<div>
<label>Username</label>
<input type="text" id="user" name="user"/>
</div>
<div>
<label>Password</label>
<input type="password" id="pass" name="pass"/>
</div>
<button type="submit">Submit</button>
</form>
<script>
document.querySelector('button')
.addEventListener('click', (e) => {
e.preventDefault();
const username = document
.querySelector('#user').value;
const password = document
.querySelector('#pass').value;
fetch('/', {
method: 'POST',
headers: {
Authorization: 'mahi',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username,
password,
}),
})
.then((res) => {
return res.json();
})
.then((data) => console.log(data));
});
</script>
</body>
</html>
Run :-
File Name :
c:\mynode_project> node myapp.js
Test On browser
File Name :
in URL :- http://localhost:3000
URL Module :-
we can get URL parameters in Node.js by importing the url module.
The get() method of URLSearchParams object gets the value of the given url parameter.
File Name :
const url = require('url');
// new URL object
const current_url = new URL('http://ittutorial.in/controller/method?id=07&name=sana');
// get access to URLSearchParams object
const params = current_url.searchParams;
// get url parameters
const id = params.get('id');
const name = params.get('name');
console.log(id);
console.log(name);
Getting Values of Multi-Valued Parameters
The getAll() method of URLSearchParams object returns an array of values of the given parameter.
File Name :
const url = require('url');
const current_url = new URL('http://ittutorial.in/controller/method?id=07&name=sana');
const search_params = current_url.searchParams;
const params = search_params.getAll('tags');
// [ "07", "sana" ]
console.log(params );
Checking If Parameter Present or not :-
The has() method checks given parameter exists in the url.
File Name :
const url = require('url');
const current_url = new URL('http://ittutorial.in/controller/method?id=07&name=sana');
const search_params = current_url.searchParams;
// true
if(search_params.has('id')) {
//
}
// false
if(search_params.has('type')) {
//
}
req.query
File Name :
var id = req.query.id; // $_GET["id"]
######################## OR ######################
var url = require('url');
var params = url.parse(request.url, true);
var query = params.query;
File Name :
app.get('/user/:id', function(req, res) {
res.send('user' + req.params.id);
});
Previous
Next