Use Express to retrieve POST query parameters

Learn how to retrieve POST query parameters using Express

POST query parameters are sent by the HTTP client (for example, via a form) or when sending data when performing a POST request.

How do you access this data?

If the data is sent asJSON format, UseContent-Type: application/json, You will useexpress.json()Middleware:

const express = require('express')
const app = express()

app.use(express.json())

If the data is sent byContent-Type: application/x-www-form-urlencoded, You need to useexpress.urlencoded()Middleware:

const express = require('express')
const app = express()

app.use(express.urlencoded({ extended: true }))

In both cases, you can access the data by referencing the data in the following ways:Request.body:

app.post('/form', (req, res) => {
  const name = req.body.name
})

Note: Earlier Express version needs to be usedbody-parserModule for processing POST data. This is no longer the case with Express 4.16 (released in September 2017) and later.

Download mine for freeExpress.js manual


More crash tutorials: