Modifying data in Server

So far we have sent small query data or id values through URL. But those are not enough to send large data set to the server. Also, those formats are not flexible enough to send different types of data like

  • JSON
  • Files
  • HTML, XML, etc.,

Request body

We need to send the information through the request body. To do that, we can't use the GET based HTTP request. We need to either use one of the three POST, PUT, or PATCH.

  • POST - to send new data
  • PUT - to update existing data
  • PATCH - to update small parts of existing data or bulk data in the server. (Rarely used though, PUT will get used for all kinds of updates in most APIs)

The browser URL bar will always send an HTTP GET request. To send anything other than GET, you need at least a browser client. Else you need a front-end client application that sends the HTTP request to the server.

We aren't going to build any frontend. We will use a browser client application called postman for testing. You can download postman here or use postman web on the browser as well,

Getting started with postman is out of scope for this course. There are good online resources available for the postman. Insomnia is a similar tool to the postman. It is up to your preference.

Postman UI

Receiving data in the server

To receive data in the express server, we need a package called body-parser. This package parses the request body and able to read the body data in the req.body.

Let's install body-parser,

npm install --save body-parser

Now in the server file add few lines to enable body-parser,

const express = require("express");
const bodyParser = require("body-parser");

const app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json

We have added support for two types of body content,

  • URL encoded
  • JSON content

These are the most used request body format. Then accepting FormData format, which is for sending files to the server.