A REST API (Web API) is an application programming interface that allows us for interaction with RESTful web services or app services. REST stands for representational state transfer. It was created by computer scientist Roy Fielding. Here we will create REST API in Nodejs.
It uses HTTP requests to access and use data. The most common HTTP requests are GET, POST, PUT, DELETE.
REST API is useful in
- Web Services
- App Services
- Cross Platform Applications
- Cloud Applications
- Stateless Applications
- Communicate different devices or platforms
REST API supports the Data formats
- application/json
- application/xml
- multipart/form-data
- application/x-wbe+xml
- application/x-www-form-urlencoded
Securing REST APIs
- Using HTTPS
- Blocking access from unknown IP addresses and domains
- Validating Input and Output
- Blocking unexpectedly large payloads
- Logging requests
- Investigating failures
- Sending Status Codes like 200,400 etc
- Validating Clients and Devices
- Authorizing API
- Authenticate URLs
- Using Headers Authorization
Create REST API in Nodejs
Step1 : Create a folder database in root directory and create a file users.json. Paste the code given below.
{
"user1" : {
"id" : 1,
"name" : "Chetan"
}
}
Step 2 : Create a file api.js and paste the code given below.
var express = require("express");
var app = express();
var fs = require('fs');
/*get api*/
app.get('/list-users', function(req, res){
fs.readFile(__dirname+'/database/users.json','utf8', function(err,data){
res.end(data);
});
});
/*post api*/
app.post('/create-user', function(req,res){
fs.readFile(__dirname+'/database/users.json', 'utf8', function(err,data){
data = JSON.parse(data);
data["user2"] = {'id' : 2, "name": "Pankaj"};
data = JSON.stringify(data);
fs.writeFile(__dirname+'/database/users.json', data,function(res){
console.log(res);
});
res.end(data);
});
});
/*delete data*/
app.delete('/delete-user/:id', function(req, res){
fs.readFile(__dirname+'/database/users.json', function(err,data){
data = JSON.parse(data);
delete data['user'+req.params.id];
data = JSON.stringify(data);
fs.writeFile(__dirname+'/database/users.json',data,function(res){
console.log(res); //it is error response
});
res.end(data);
});
});
/*create server*/
var server = app.listen(8081, function(){
var host = server.address().address;
var port = server.address().port;
console.log('Server Running at http://%s:%s',host,port);
});
Step 3 : Install Express Node Package
Use these commands one by one
npm install express --save
npm install body-parser --save
npm install cookie-parser --save
npm install multer --save
Step 4 : Now run Node.js application
Use this command to run
node api.js
Step 5 : To test the api run the following URLs
http://localhost:8081/list-users
http://localhost:8081/create-user
http://localhost:8081/delete-user/1
You can replace localhost with your domain or IP
Recommended :
Read and Write Streams in Node.js
For more NodeJS Tutorials visit NodeJS page.
If you like this, share this.
Follow us on Facebook, Twitter, Tumblr, LinkedIn, YouTube.