Rest API in 5 mins using Express

Tannishk sharma
Nov 9, 2018 · 3 min read
Its an API that uses HTTP requests 

1. Client-Server

2. Stateless

3. Cache

The most efficient network request is one that does not use the network.

4. Uniform Interface

5. Layered System

6. Code On Demand

npm install --save express

A simple Hello World on express var express = require('express');
var app = express();
app.get('/',function(req,res){
res.end('Hello World');
})
var server = app.listen(3000,function(){
console.log("Server is running at 3000 port");
})
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());app.all('/dishes', (req,res,next) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
next();
});
app.get('/dishes', (req,res,next) => {
res.end('Will send all the dishes to you!');
});
app.post('/dishes', (req, res, next) => {
res.end('Will add the dish: ' + req.body.name + ' with details: ' + req.body.description);
});
app.put('/dishes', (req, res, next) => {
res.statusCode = 403;
res.end('PUT operation not supported on /dishes');
});

app.delete('/dishes', (req, res, next) => {
res.end('Deleting all dishes');
});
app.get('/dishes/:dishId', (req,res,next) => {
res.end('Will send details of the dish: ' + req.params.dishId +' to you!');
});
app.post('/dishes/:dishId', (req, res, next) => {
res.statusCode = 403;
res.end('POST operation not supported on /dishes/'+ req.params.dishId);
});
app.put('/dishes/:dishId', (req, res, next) => {
res.write('Updating the dish: ' + req.params.dishId + '\n');
res.end('Will update the dish: ' + req.body.name +
' with details: ' + req.body.description);
});
app.delete('/dishes/:dishId', (req, res, next) => {
res.end('Deleting dish: ' + req.params.dishId);
});
app.listen(3000,()=>{
console.log('Server is running at port 3000')
});

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade