How to create REST APIs in NodeJs?
REST stands for REpresentational State Transfer. REST is web standards based architecture and uses HTTP Protocol.
A REST Server simply provides access to resources and REST client accesses and modifies the resources using HTTP protocol.
Here each resource is identified by URIs/ global IDs. REST uses various representation to represent a resource like text, JSON, XML but JSON is the most popular
HTTP methods
File Name :
Following four HTTP methods are commonly used in REST based architecture.
GET − This is used to provide a read only access to a resource.
PUT − This is used to create a new resource.
DELETE − This is used to remove a resource.
POST − This is used to update a existing resource or create a new resource.
List Users
File Name : users.json
{
"user1" : {
"name" : "mahtab",
"password" : "pass",
"profession" : "tutor",
"id": 1
},
"user2" : {
"name" : "sana",
"password" : "pass2",
"profession" : "dev",
"id": 2
},
"user3" : {
"name" : "mahira",
"password" : "pass3",
"profession" : "pro",
"id": 3
}
}
File Name : myapp.js
var express = require('express');
var app = express();
var fs = require("fs");
app.get('/listUsers', function (req, res) {
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
console.log( data );
res.end( data );
});
})
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)
})
Add User
File Name :
var express = require('express');
var app = express();
var fs = require("fs");
var user = {
"user4" : {
"name" : "sara",
"password" : "pass4",
"profession" : "Doctor",
"id": 4
}
}
app.post('/addUser', function (req, res) {
// First read existing users.
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
data = JSON.parse( data );
data["user4"] = user["user4"];
console.log( data );
res.end( JSON.stringify(data));
});
})
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)
})
Show Detail
File Name : myapp.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)
})
Delete User
File Name : myapp.js
var express = require('express');
var app = express();
var fs = require("fs");
var id = 2;
app.delete('/deleteUser', function (req, res) {
// First read existing users.
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
data = JSON.parse( data );
delete data["user" + 2];
console.log( data );
res.end( JSON.stringify(data));
});
})
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)
})
Previous
Next