1

Created a Node module using Express router to support the routes for the dishes REST API.

Index.js file:

const express = require('express');
const http = require('http');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const app = express();  // here we are saying that our application are going to use express node module
const dishRouter = require('./route/dishRouter');
app.use('/dishes/:dishId', dishRouter);

const hostname = 'localhost';
const port = 3000;





app.use(bodyParser.json());  // This allows us to parse the body of the request message

app.use(morgan('dev')); //it will print out additional information to the screen as required

app.use(express.static(__dirname + '/public')); // This will serve the static files


app.use((req, res, next) => {
    console.log(req.headers);
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    res.end('<html><body><h1>This is an express server </h1></body></html>');
});

const server = http.createServer(app); // here we set up the server using express App

server.listen(port,hostname,() => {
    console.log(`Server runnig at http://${hostname}:${port}`);
})

DishRouter.js file:

const express = require('express');
const bodyparser = require('body-parser');

const dishRouter = express.Router({mergeParams:true});

dishRouter.use(bodyparser.json());

dishRouter.route('/') //mount this express router at the /dishes endpoint.chain all GET PUT POST DELET method already do this router

.all((req,res,next) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    next();
})
.get((req,res,next) => {
    res.end('Will send all the dishes to you!');
})
.post((req,res,next) => {
    res.end("Will add the dish : " + req.body.name + "  with details: " + req.body.description);
})
.put((req,res,next) => {
    res.statusCode = 403;
    res.end('PUT is not supported by /dishes');
})
.delete((req,res,next) => {
    res.end('Deleting all the dishes');
});

// Routes for specific dish 
dishRouter.route('/:dishId')
.all((req,res,next) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    next();
})
.get((req,res,next) => {
    res.end('Will send the dish ' +req.params.id +' to you!');
})
.post((req,res,next) => {
     res.statusCode = 403;
    res.end('POST operation not supported on /dishes/'+req.params.id);
})
.put((req,res,next) => {
    res.write('Updating the dish: ' + req.params.id + '\n');
    res.end("Will add the dish : " + req.body.name + " with details: " + req.body.description);
})
.delete((req,res,next) => {
    res.end('Deleting the dish '+req.params.id);
});

module.exports = dishRouter ;

This need to support GET, PUT, POST and DELETE operations on each of the endpoints. The code is properly working for dishes but For dishId it is showing same results as dishes.

When sending a GET request to localhost:3000/dishes/28, the following response is sent:

Will send all the dishes to you!

divyanshu_31
  • 65
  • 1
  • 6

1 Answers1

0

Express routes match on a first come first serve basis. So because your root route / will match any route and it is defined first, it will always use that one. Try moving your root route to the bottom of the file. i.e.

const express = require('express');
const bodyparser = require('body-parser');

const dishRouter = express.Router({mergeParams:true});

dishRouter.use(bodyparser.json());

// Routes for specific dish 
dishRouter.route('/:dishId')
.all((req,res,next) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    next();
})
.get((req,res,next) => {
    res.end('Will send the dish ' +req.params.id +' to you!');
})
.post((req,res,next) => {
     res.statusCode = 403;
    res.end('POST operation not supported on /dishes/'+req.params.id);
})
.put((req,res,next) => {
    res.write('Updating the dish: ' + req.params.id + '\n');
    res.end("Will add the dish : " + req.body.name + " with details: " + req.body.description);
})
.delete((req,res,next) => {
    res.end('Deleting the dish '+req.params.id);
});

dishRouter.route('/') //mount this express router at the /dishes endpoint.chain all GET PUT POST DELET method already do this router

.all((req,res,next) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    next();
})
.get((req,res,next) => {
    res.end('Will send all the dishes to you!');
})
.post((req,res,next) => {
    res.end("Will add the dish : " + req.body.name + "  with details: " + req.body.description);
})
.put((req,res,next) => {
    res.statusCode = 403;
    res.end('PUT is not supported by /dishes');
})
.delete((req,res,next) => {
    res.end('Deleting all the dishes');
});
module.exports = dishRouter ;
Wodlo
  • 847
  • 5
  • 8