I want to use a single API endpoint for both GET and POST requests.
My goal is as follows:
- Send multiple POST requests to /api/users with data like: {'id': 2, is_valid: 'true'}
- Retrieve this data by fetching the same API URL later on and display it in my application.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 5000;
const jsonParser = bodyParser.json();
app.all('/api/users', jsonParser, (req, res) => {
const users = [
{id: '1', is_valid: false}
];
users.push(req.body);
res.json(users);
});
However, whenever I fetch this endpoint, it always shows the original users array. The data I added to my array never gets saved.