Request Parameters in Express

The first parameter to Express route handlers and middleware functions is the Express request object. This parameter is usually called req .

const express = require ( 'express' ); const app = express(); app.get( '*' , function ( req, res ) { req instanceof require ( 'http' ).IncomingMessage; res.json({ ok : 1 }); }); const server = await app.listen( 3000 );

Request Parameters

Express parses query string parameters by default, and puts them into the req.query property.

const axios = require ( 'axios' ); const express = require ( 'express' ); const app = express(); app.get( '*' , function ( req, res ) { const name = req.query.name; const rank = req.query.rank; res.json({ name, rank }); }); const server = await app.listen( 3000 ); const querystring = 'name=Jean-Luc Picard&rank=Captain' ; const res = await axios.get( 'http://localhost:3000?' + querystring); res.data.name; res.data.rank;

Express also supports named route parameters and puts them in the req.params object. Named route parameters are always strings, and Express automatically decodes them using decodeUriComponent() .

const axios = require ( 'axios' ); const express = require ( 'express' ); const app = express(); app.get( '/:model/:id' , function ( req, res ) { const model = req.params.model; const id = req.params.id; res.json({ model, id }); }); const server = await app.listen( 3000 ); const res = await axios.get( 'http://localhost:3000/user/1' ); res.data.model; res.data.id;

Express does not parse the request body for you by default. To opt in to parsing JSON request bodies, use the express.json() middleware. Express will then parse the HTTP request body and put the parsed body in req.body .

const axios = require ( 'axios' ); const express = require ( 'express' ); const app = express(); app.use(express.json()); app.put( '*' , function ( req, res ) { const name = req.body.name; const rank = req.body.rank; res.json({ name, rank }); }); const server = await app.listen( 3000 ); const body = { name : 'Jean-Luc Picard' , rank : 'Captain' }; const res = await axios.put( 'http://localhost:3000' , body); res.data.name; res.data.rank;

Headers

To get the value of an HTTP request header, you should use Express' req.get() function. You may also use Node.js' native req.headers property.

const axios = require ( 'axios' ); const express = require ( 'express' ); const app = express(); app.get( '*' , function ( req, res ) { const authorization = req.get( 'authorization' ); req.headers.authorization; res.json({ authorization }); }); const server = await app.listen( 3000 ); const res = await axios.get( 'http://localhost:3000' , { headers : { 'Authorization' : 'test' } }); res.data.authorization;

Here's a tutorial on how to set request headers in Axios if you're unfamiliar with Axios.

Body Size Limit

By default, express.json() limits the request body to 100kb. If the request body is any larger, Express will throw an HTTP 413 "Payload Too Large" error. You can configure this limit using the limit option to express.json() .

const axios = require ( 'axios' ); const express = require ( 'express' ); const app = express(); app.use(express.json({ limit : 10 })); app.put( '*' , function ( req, res ) { const name = req.body.name; const rank = req.body.rank; res.json({ name, rank }); }); const server = await app.listen( 3000 ); const body = { name : 'Jean-Luc Picard' , rank : 'Captain' }; const err = await axios.put( 'http://localhost:3000' , body). catch( err => err); err.response.status;

Want to become your team's Express expert? There's no better way to really grok a framework than to write your own clone from scratch. In 15 concise pages, this tutorial walks you through how to write a simplified clone of Express called Espresso. Get your copy!



Espresso supports: Route handlers, like `app.get()` and `app.post()`

Express-compatible middleware, like `app.use(require('cors')())`

Express 4.0 style subrouters As a bonus, Espresso also supports async functions, unlike Express.



Get the tutorial and master Express today!

More Express Tutorials