The differences between req.params, req.query, and req.body in Express.js & Node.js
Fabio Jonathan Arifin
Posted on February 12, 2024
In this article, I’ll cover the fundamentals of Express & Node.js
in regards to req.params, req.query, and req.body. When we discuss how websites operate, we often mention terms like “request” and “response.” A “request” is what a user asks for, and the “response” is what the server sends back.
In Node.js programming, we handle requests and responses manually, even with frameworks like Express.js. Here’s how the server communication works:
We start by calling the web server provided by Node.js.
We read the URL the user wants to visit and decide how to handle it.
We process the request from the user.
We send back a response to the user.
Essentially, both parameters (request and response) come from http.createServer, an HTTP server provided by Node.js that handles web communication (request and response). When http.createServer is executed upon a request, it provides two parameters:
request, which is an instance of http.IncomingMessage, representing the HTTP Request sent by the user. A single connection can generate multiple HTTP Requests. We'll get more into this later.
response, an instance of http.ServerResponse, representing the HTTP Response that will be sent back to the user.
When we make a request (request), we sometimes want to either request data from the server or send data to the server, whether through a URL or a form (HTML). In this article, we’ll discuss sending and retrieving values sent via requests.
req.query
: When data is sent through a URL, like http://localhost:3000/search?name=John&age=45
, we can retrieve that data in our Node.js/Express code like this:
router.get('/search', function(req, res, next) {
var name = req.query.name;
console.log(`Name: ${name}`)
var age = req.query.age;
console.log(`Age: ${age}`)
res.send('Request has been parsed, see console');
-
req.params
: If data is sent directly in the URL without keys, likehttp://localhost:3000/search/John/45
, we can get it like this:
router.get('/search/:name/:age', function(req, res, next) {
var name = req.params.name;
console.log(`Name: ${name}`)
var age = req.params.age;
console.log(`Age: ${age}`)
res.send('Request has been parsed, see console');
});
-
req.body
: When data is sent through an HTML form, we usereq.body
. We first need to use thebody-parser
library to handle this data. Here’s how:
// Calling the library
var bodyParser = require('body-parser');
// Using the library in Express
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
Then, we can get the data from the form this way:
router.post('/search', function(req, res, next) {
var name = req.body.name;
console.log(`Name: ${name}`)
var age = req.body.age;
console.log(`Age: ${age}`)
res.send('Request has been parsed, see console');
});
That’s it! That’s how we handle data sent through URLs and HTML forms in Node.js.
Posted on February 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
February 12, 2024
February 10, 2021