Embarking on my journey with express JS sans experience, I encountered a roadblock here:
server.js:
const express = require('express');
const router = express.Router();
const app = express();
app.use(express.json());
const users = []
router.get('/getUsers', (req, res) => {
res.json(users);
});
router.post('/postUsers', (req, res) => {
const newUser = req.body;
console.log(newUser);
users.push(newUser);
res.json(`${req.body.name} is added`);
});
app.use('/', router);
app.listen(3000);
test.rest:
###
GET http://localhost:3000/getUsers
###
POST http://localhost:3000/postUsers
{
"name": "Joseph"
}
###
Upon making a post request, the response reads as: "undefined is added" with a 200 status.
When initiating a get request, the response is as follows:
[
{}
]
with a 200 status
I reached out to ChatGPT for assistance, but unfortunately, the issue remains unresolved. Apologies if this query seems trivial, as I am genuinely struggling to comprehend the problem at hand.