Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 5, 2022 09:27 am GMT

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

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To