Now we will implement an API which will be called using user ID and it will display the detail of the corresponding user.
server.js
var express = require(‘express’);
var app = express();
var fs = require(“fs”);
app.get(‘/:id’, function (req, res) {
// First read existing users.
fs.readFile( __dirname + “/” + “users.json”, ‘utf8’, function (err, data) {
var users = JSON.parse( data );
var user = users[“user” + req.params.id]
console.log( user );
res.end( JSON.stringify(user));
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log(“Example app listening at http://%s:%s”, host, port)
})
Now try to access defined API using URL: http://127.0.0.1:8081/2 and HTTP Method : GET on local machine using any REST client. This should produce following result −
{“name”:”suresh”,”password”:”password2″,”profession”:”librarian”,”id”:2}