I am trying to implement an in-memory REST API using Nodejs and Express4.
I have no problem listing users and getting a user by id. However, I am having troubles with POST operation.
Edit: As suggested in the comments, I am now using req.body to pass the JSON data that is received in the request, but "null" is added to my array instead of the JSON data. I am not sure if this is a problem in my code or due to the way I use Postman.
Below is how I am using Postman to send the data **
(I have removed the comma after the last value:)**:

Below is my code:
user.js
var users = [{"id": "1", "firstName": "John", "lastName": "Doe"}];
exports.getAllUser = function() {
return users;
};
exports.getUserById = function(id) {
for (var i = 0; i < users.length; i++) {
if (users[i].id == id) return users[i];
}
};
exports.deleteUser = function(id) {
for (var i = 0; i < users.length; i++) {
if (users[i].id == id) {
users.splice(i,1);
}
}
}
exports.updateUser = function(id, json) {
for (var i = 0; i < users.length; i++) {
if (users[i].id == id) {
users.splice(i, 1, json);
}
}
}
exports.addUser = function(json) {
users.push(json);
}
server.js
var express = require('express');
var users = require('./user');
var app = express();
var port = process.env.PORT || 8080;
var router = express.Router();
router.use(function(req, res, next) {
console.log('%s %s', req.method, req.path);
next();
});
router.get('/users', function(req, res, next) {
res.json({ users: users.getAllUser() });
});
router.get('/users/:id', function(req, res, next) {
var user = users.getUserById(req.params.id)
res.json(user);
});
router.put('/users/:id', function(req, res, next) {
users.updateUser(req.params.id, req.body);
});
router.delete('/users/:id', function(req, res, next) {
users.deleteUser(req.params.id);
});
router.post('/users', function(req, res, next) {
users.addUser(req.body);
});
// Only requests to /api/ will be send to router.
app.use('/api', router);
app.listen(port);
console.log('Server listening on port ' + port);
Regarding the POST, how should I pass the JSON from the request to the array? I guess the answer to that question would also help me with performing a PUT (an update). I would have 2 parameters, one would be the ID, the other would be the JSON data?
Additionally, how to test the POST with Postman? Where do I specify the JSON to send?