DEV Community

Cover image for The differences between req.params, req.query, and req.body in Express.js & Node.js
Fabio Jonathan Arifin
Fabio Jonathan Arifin

Posted on

The differences between req.params, req.query, and req.body in Express.js & Node.js

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:

  1. We start by calling the web server provided by Node.js.

  2. We read the URL the user wants to visit and decide how to handle it.

  3. We process the request from the user.

  4. 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');
Enter fullscreen mode Exit fullscreen mode
  1. req.params: If data is sent directly in the URL without keys, like http://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');
});
Enter fullscreen mode Exit fullscreen mode
  1. req.body: When data is sent through an HTML form, we use req.body. We first need to use the body-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())
Enter fullscreen mode Exit fullscreen mode

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');
});
Enter fullscreen mode Exit fullscreen mode

That’s it! That’s how we handle data sent through URLs and HTML forms in Node.js.

Top comments (0)