An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
How to make POST Requests in Express
Introduction
In this blog article, we shall learn how to make POST requests in Express.
POST HTTP request uses the POST method and is mostly used when sending some data along with the request to the HTTP server.
In Express youll need to enable a middleware to parse the body of Content-type: application/json
. This enables parsing incoming JSON content in the body of the incoming request.
Values sent in the POST request are populated inside the req.body
object.
A Simple Express application
Let's setup a simple Express application
const express = require('express')const app = express()// enable middleware to parse body of Content-type: application/jsonapp.use(express.json())app.post('/', (req, res) => { // get request values inside req.body const price = req.body.price const orderId = req.body.orderId // use price, orderId to do something meaningful})
Requests are client constructed values and should be sanitized and validated before use once they get to the Express application.
Summary
To handle POST requests in Express we need to enable parsing of json by enabling the json middleware.
Found this article helpful? You may follow my handle on twitter @nkmurgor where I tweet about interesting topics on web development.
Original Link: https://dev.to/naftalimurgor/how-to-make-post-requests-in-express-2be6
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To