Express js Request

Express request is a wrapper for node.js http.request object. These are the list of some properties of the request object.

req.params

The req.params is used to get the url path, query strings from within a specific request handler. This is basically an array with key value pairs.

Example

var express = require("express");
var app = express();

app.get('/params/:type/:name/:value', function(req, res) {
console.log(req.params);
res.end();
})

app.listen(3030);
http://localhost:3030/params/express/solo/121

When we run the code and open at the above url, we will get the following -


Express.js Request

req.body

This object contains text parameters from the parsed request body. It is popular with body-parser middleware.

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

app.post('/body', function(req, res){
 console.log(req.body);
 res.end(JSON.stringify(req.body)+'\n');
});

req.files

This req.files is used to handle multipart file uploads. It is used with express.bodyParser() or express.multipart() middlewares.

app.post('/upload', function(req, res){
	console.log(req.files.archive);
	res.end();
 })

req.route

The req.route object has the current matched route's information. The information can be the following -

  • path: request URL pattern
  • method: HTTP method of the request
  • keys: list of parameters in the URL pattern
  • regexp: generated pattern for the path
  • params: req.params object
app.get('/params/:role/:name/:status', function(req, res) {
 console.log(req.route);
 res.end();
});

req.cookies

This is used to get the request cookies with req.cookies object. This is enabled with express.cookieParser() middleware.

req.cookies.session_id