Building REST APIs with ExpressJS and Nodejs Functions - POST and PUT
I am trying to implement an in-memory REST API using Nodejs and Express4.
I have no problem listing users and getting the user by id. However, I am having problems with the POST operation.
Edit: As suggested in the comments, I am now using req.body to pass the JSON data received in the request, but "null" is added to my array instead of the JSON data. I'm not sure if this is a problem in my code or because of the way I use Postman.
Below is how I use Postman to send data ** (I 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);
As far as POST is concerned, how do I pass JSON from request to array? I think the answer to this question would also help me with PUT (update). I would have 2 parameters, one would be ID, the other would be JSON data?
Also, how do I check POST with Postman? Where can I specify the JSON to send?
source to share
When you post json to the http body, you must set the correct HTTP Content-Type header regardless of the http method. In the case of json, this is "application / json". You have to add this header to the postman. This header signals that the HTTP body of the received request has a valid serialized json string. To parse the http body automatically, you can use middleware like https://github.com/expressjs/body-parser . This middleware tries to parse the received HTTP object according to the "Content-Type" header - in your case json. If this expressed middleware manages to parse the body as json, it populates its "req.body".
source to share