Front-end sends a JSON array; the back-end receives it via the POST method using Express.js.
Answer:
When the front-end sends a JSON array, the back-end typically receives it via the POST HTTP method. The data is accessed in Express.js through req.body after parsing with middleware like express.json(). Here’s a concise example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const express = require(‘express’); const app = express(); app.use(express.json()); app.post(‘/api/users’, (req, res) => { const dataArray = req.body; // Process dataArray as needed res.send({ message: ‘Data received successfully!’ }); }); app.listen(3000, () => { console.log(‘Server is running on http://localhost:3000’); }); |
Explanation:
-
Front-end sends JSON array: The client (front-end) uses fetch or an HTTP library to send a POST request with the JSON body.
-
Back-end setup:
- Include express.json() middleware in your Express app to parse incoming JSON data into req.body.
-
Create a POST route that handles the request and processes the received array.
-
Accessing Data: The sent JSON array is available as req.body inside your route handler, allowing you to work with it easily.
This setup ensures smooth communication between front-end and back-end when sending structured data like JSON arrays.
Leave a Reply
You must be logged in to post a comment.