- Express.js Basics
- Express.js HOME
- Express.js Introduction
- Express.js Installation
- Express.js Basic App
- Express.js Routing
- Basics Routing
- Route Parameters
- Handling Query Strings
- Router Middleware
- Middleware
- What is Middleware?
- Application-Level Middleware
- Router-Level Middleware
- Built-In Middleware
- Error-Handling Middleware
- Third-Party Middleware
- Express.js HTTP
- Handling GET Requests
- Handling POST Requests
- Handling PUT Requests
- Handling DELETE Requests
- Templating Engines
- Using Templating Engines
- Setting Up EJS
- Setting Up Handlebars
- Setting Up Pug
- Request/Response
- Request Object
- Response Object
- Handling JSON Data
- Handling Form Data
- Static Files
- Serving Static Files
- Setting Up Static Folders
- Managing Assets
- Express.js Advanced
- Middleware Stack
- CORS in Express.js
- JWT Authentication
- Session Handling
- File Uploads
- Error Handling
- Databases
- Express.js with MongoDB
- MongoDB CRUD Operations
- Express.js with MySQL
- MySQL CRUD Operations
- Deployment
- Deploying Express.js Apps to Heroku
- Deploying Express.js Apps to AWS
- Deploying Express.js Apps to Vercel
Express.js Handling JSON Data
Handling JSON (JavaScript Object Notation) data is arguably the most important skill for a modern web developer. Since JSON is the standard language of the web, almost every Single Page Application (SPA), mobile app, and external service communicates using this format. Express.js provides built-in, streamlined methods to receive, process, and send JSON data, making it a powerful choice for building RESTful APIs.
Key Features of Handling JSON Data
- Request Body Parsing: Express simplifies the process of intercepting raw text strings from a client and turning them into accessible JavaScript objects.
- Sending JSON Responses: You can convert complex data structures, such as arrays of database results or nested objects, into JSON strings with a single method call.
- Middleware Support: Express includes a built-in parsing engine, meaning you don't need to install third-party libraries like
body-parserfor basic JSON handling in modern versions of the framework.
express.json() middleware at the top of your middleware stack to ensure all subsequent routes can access parsed data.
Components of Handling JSON Data
Parsing JSON Request Body
By default, Express does not parse the body of incoming requests. To handle JSON data sent via a POST, PUT, or PATCH request, you must register the express.json() middleware. Once registered, any incoming JSON is parsed and attached to the req.body object.
Content-Type: application/json, Express might not parse the body correctly, and req.body will likely be undefined or empty.
Example:
const express = require('express');
const app = express();
// Use express.json() middleware - This is essential!
app.use(express.json());
app.post('/api/users', (req, res) => {
// Imagine the client sent: { "name": "John", "email": "[email protected]" }
const newUser = req.body;
console.log(newUser.name); // Output: John
res.send('User created');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
In this example, when a POST request is made to /api/users, the server automatically transforms the raw JSON string into a usable object stored in req.body.
app.use(express.json()). Without this line, req.body will be undefined, and your application will likely crash when you try to access a property on it.
Sending JSON Responses
When building an API, you rarely send plain text. Instead, you send JSON so that the front-end (like React or Vue) can easily parse the data. The res.json() method is perfect for this; it converts your JavaScript object to a JSON string and automatically sets the correct HTTP headers.
Example:
app.get('/api/product', (req, res) => {
const product = {
id: 101,
name: 'Wireless Mouse',
price: 29.99,
inStock: true
};
res.json(product); // Automatically sets Content-Type to application/json
});
This method is smarter than res.send() because it ensures the client knows exactly what type of data it is receiving.
Handling JSON Errors
If a client sends "broken" JSON (for example, forgetting a closing quote or a comma), express.json() will fail and throw a SyntaxError. If you don't handle this, the server might expose a messy stack trace to the user.
Example:
app.use(express.json());
// Global error handler for malformed JSON
app.use((err, req, res, next) => {
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
return res.status(400).json({
success: false,
message: 'The JSON sent is malformed. Please check your syntax.'
});
}
next();
});
Sending JSON with Status Codes
In a professional API, the status code tells the client the "result" of their request. Use res.status() in combination with res.json() to provide meaningful feedback.
- 201 Created: Use after successfully saving data.
- 400 Bad Request: Use when the client sends invalid data.
- 404 Not Found: Use when a requested resource doesn't exist.
Example:
app.post('/api/login', (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password are required' });
}
// Logic to verify user...
res.status(200).json({ message: 'Login successful', token: 'abc-123' });
});
success boolean or a data object) so the front-end developers know exactly what to expect.
Example Code
const express = require('express');
const app = express();
// 1. Enable JSON parsing middleware
app.use(express.json());
// 2. Sample Route: Receiving data
app.post('/api/feedback', (req, res) => {
const { user, comment } = req.body;
if (!user || !comment) {
return res.status(400).json({ error: 'Please provide both user and comment.' });
}
console.log(`Received feedback from ${user}: ${comment}`);
res.status(201).json({ message: 'Feedback saved successfully!' });
});
// 3. Sample Route: Sending data
app.get('/api/status', (req, res) => {
res.json({
status: 'Online',
uptime: process.uptime(),
version: '1.0.0'
});
});
// 4. Catch-all Error Handling for JSON Syntax Errors
app.use((err, req, res, next) => {
if (err instanceof SyntaxError) {
return res.status(400).json({ error: 'Invalid JSON format in request body' });
}
next(err);
});
// Start the server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Summary
Handling JSON in Express.js is a straightforward process once you understand the flow of data. By using express.json() middleware, you unlock the ability to read req.body. By using res.json(), you ensure your server communicates professionally with modern clients. Remember to always validate incoming data and handle potential syntax errors to keep your API robust and user-friendly.