Handling Different Types of Data in Express.js

posted 4 min read

In my previous two articles, we covered how to connect MongoDB with Node.js using Mongoose, and why it’s important to start the server only after a successful database connection.

Now, let’s assume that your server is up and running. The next logical step is to understand what happens when your server starts receiving requests.

So the big question is:
What kind of data can your server receive, and how do you handle it in Express.js?

In this article, we’ll break down the different types of data that can come from the frontend and how to properly parse them using the right middlewares.

Most of our work today will be done inside the app.js file the same file where we’ve written our Express server setup. Till now, our app.js file looks something like this:

import express from "express"

const app = express();

export default aap;

What Type of Data Does Your Server Receive?

Now that our server is up and running, let’s talk about the most important part:

The server is going to receive data — but what kind of data?

Whether it’s coming from the frontend (like a React app), an HTML form, an API testing tool like Postman, or even a third-party webhook — your Express server needs to be ready to handle and understand different types of incoming data.

In Express.js, there are a few common formats that your server might receive:

  1. JSON - From frontend apps or APIs (Content-Type: application/json);\
  2. URL-encoded form data – From HTML forms (Content-Type: application/x-www-form-urlencoded)
  3. Cookies – Automatically sent by browsers with requests.
  4. Plain text – Sometimes sent by APIs, tools like curl, or raw fetch calls (Content-Type: text/plain)
  5. (Later) File uploads (multipart/form-data) – which we’ll handle using multer in future articles.

Now let’s go one by one and learn how to make Express understand each type using the right middleware.

Packages You’ll Need

Before we move forward, make sure you have these two packages installed — we’ll be using them shortly:

npm install cors cookie-parser

I’ll explain each one in detail as we move ahead.

Now that we know the server can receive different types of data — one question comes up:

What is req.body?

Whenever the client (like a React app, form, or Postman) sends data to your server — that data is sent in the body of the request.

In Express, we access data using:

req.body

But, By default, req.body is undefined.

Express doesn’t understand what’s inside the body unless we tell it how to read it.

Now Let’s See — How Does the Server Understand req.body?

Okay, so we know that data comes to the server in the request body.
But how does Express actually understand and extract that data?

The answer is: using the body-parser middleware.

Earlier, we had to install it manually, but now — you don’t need to install anything separately.

Express has this functionality built-in, so you can directly use it .To use a middleware we use

app.use()function

app.use(express.json({limit : "16kb"}));

As mentioned above we used app.use() function to use a middleware

The express.json middleware is important for parsing incoming JSON payloads and making that data available in the req.body, *Without using express.json, Express will not automatically parse the JSON data in the request body.*.By using the express.json middleware, you can handle POST, PUT, or PATCH requests that send JSON data from the client to the server.

The limit property in express.json() is used to control the maximum size of the JSON payload that can be parsed by the middleware.

app.use(express.urlencoded({ extended: true , limit : "16kb" })); // To read HTML form data

The express.urlencoded() middleware in Express.js is used to parse URL-encoded form data, making it accessible as a JavaScript object in req.body. It's essential for handling form submissions in application/x-www-form-urlencoded format.

 app.use(express.static("public"))

The express.static() is a built-in middleware function in Express.js that allows you to serve static files (like images, HTML, CSS, and JavaScript) directly to the client. It automatically makes all files inside a specified folder accessible via HTTP requests. You don’t have to create custom routes for each file.

 app.use(cookieParser());

Now lets talk about cookie parser which we installed earlier and make sure before using cookie parser you have corrcetly imported it like this:

import cookieParser from "cookie-parser";

Sometimes, the client sends data in cookies along with the request — for example, authentication tokens, user preferences, or session IDs.

Since this is also a form of client-sent data (just like body or URL parameters), we need to handle and parse cookies on the server-side.

That's why we use middleware like cookie-parser in Express. It allows us to access cookie values through req.cookies so we can properly handle sessions, user authentication, or any logic based on the data stored in cookies.

If you remember we have also installed cors lets talk about it also:

What is CORS?

CORS (Cross-Origin Resource Sharing) is a security feature built into web browsers.

It controls whether a web page from one origin (domain, port, or protocol)
can make a request to a server on another origin.

Now let’s say:

Your frontend runs on: http://localhost:3000

Your backend API runs on: http://localhost:5000

Even though both are on localhost, the ports are different, so the browser considers them different origins.

Now if the frontend makes a request like:

fetch("http://localhost:5000/api/user")

The browser will block it — unless the backend gives permission using CORS headers.

NOW use the cors middleware

app.use(cors({
  origin: "http://localhost:3000", // your frontend origin
  credentials: true                // allow cookies if needed
}));

Now you can easily make request on this origin

Wrapping Up
In this article, we explored how your Express.js server can receive and handle different types of data coming from the frontend — whether it's JSON, form data, cookies, or even static file requests. We also covered how to configure your server using the right middleware to make sense of that data.

By setting up these middlewares correctly in your app.js, you're making your backend flexible, secure, and ready to communicate with any frontend — whether it's a React app, Postman, or a mobile client.

In the next article, we’ll go a step further and look into how to handle file uploads,

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Its been a while since I used express, great write-up.

More Posts

Clean, maintainable code practices in Node.js and Java with real-world examples and principles.

Nigel DSouza - Jun 27

How to Connect MongoDB to Node.js with Mongoose (Step-by-Step Guide)

Riya Sharma - Jun 14

How to set up TypeScript with Node.js and Express (2025)

Sunny - Jun 6

How Load Balancers Keep Websites Fast and Reliable

Riya Sharma - Jan 28

Next.js Almost Broke Me - Here's the Survival Guide They Won't Give You

Sourav Bandyopadhyay - May 24
chevron_left