logoEframix

Parsing JSON in Requests

Learn how to use Eframix's built-in bodyParser middleware to handle JSON data in your application.

What is JSON Parsing?

JSON parsing is the process of converting JSON-formatted data from the request body into a JavaScript object. This allows your server to access and manipulate the data.

In Eframix, the bodyParser middleware provides an easy way to parse JSON data in incoming requests, allowing your application to work with JSON data directly.

Using the bodyParser Middleware

The bodyParser middleware in Eframix parses the request body as JSON, making it accessible through req.body. Here’s how to add it to your application.

import Router from 'eframix';
const app = new Router();
app.use(app.bodyParser);
app.post('/api/movies', (req, res) => {
const newMovie = req.body;
console.log("New Movie:", newMovie);
res.status(201).send({ message: 'Movie added successfully', data: newMovie });
});
app.startServer(5001, () => {
console.log('Server is running on http://localhost:5001');
});

Now, any JSON data sent in the request body will be automatically parsed and available as req.body.