Retrieve the POST query parameters using Express
Found out how to retrieve the POST query parameters using Express
POST query parameters are sent by HTTP clients for example by forms, or when performing a POST request sending data.
How can you access this data?
If the data was sent as JSON, using Content-Type: application/json
, you will use the express.json()
middleware:
const express = require('express')
const app = express()
app.use(express.json())
If the data was sent using Content-Type: application/x-www-form-urlencoded
, you will need to use the express.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 it from Request.body
:
app.post('/form', (req, res) => {
const name = req.body.name
})
Note: older Express versions required the use of the
body-parser
module to process POST data. This is no longer the case as of Express 4.16 (released in September 2017) and later versions.
I wrote 21 books to help you become a better developer:
- HTML Handbook
- Next.js Pages Router Handbook
- Alpine.js Handbook
- HTMX Handbook
- TypeScript Handbook
- React Handbook
- SQL Handbook
- Git Cheat Sheet
- Laravel Handbook
- Express Handbook
- Swift Handbook
- Go Handbook
- PHP Handbook
- Python Handbook
- Linux Commands Handbook
- C Handbook
- JavaScript Handbook
- Svelte Handbook
- CSS Handbook
- Node.js Handbook
- Vue Handbook