Skip to content

Instantly share code, notes, and snippets.

@MCheli
Created June 13, 2016 21:42
Show Gist options
  • Save MCheli/c4efc94f0d754f3e497e13b1bbf04b0d to your computer and use it in GitHub Desktop.
Save MCheli/c4efc94f0d754f3e497e13b1bbf04b0d to your computer and use it in GitHub Desktop.
Minimal Node JS Server with Express Routers as Modules
var express = require('express');
var bodyParser = require('body-parser');
var dishRouter = express.Router();
dishRouter.use(bodyParser.json());
dishRouter.route('/')
.all(function (req, res, next) {
res.writeHead(200, {'Content-Type': 'text/plain'});
next();
})
.get(function (req, res, next) {
res.end('Will send all the dishes to you!');
})
.post(function (req, res, next) {
res.end('Will add the dish: ' + req.body.name + ' with details: ' + req.body.description);
})
.delete(function (req, res, next) {
res.end('Deleting all dishes');
});
dishRouter.route('/:dishId')
.all(function (req, res, next) {
res.writeHead(200, {'Content-Type': 'text/plain'});
next();
})
.get(function (req, res, next) {
res.end('Will send details of the dish: ' + req.params.dishId + ' to you!');
})
.put(function (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);
})
.delete(function (req, res, next) {
res.end('Deleting dish: ' + req.params.dishId);
})
module.exports = dishRouter;
var express = require('express');
var bodyParser = require('body-parser');
var leaderRouter = express.Router();
leaderRouter.use(bodyParser.json());
leaderRouter.route('/')
.all(function (req, res, next) {
res.writeHead(200, {'Content-Type': 'text/plain'});
next();
})
.get(function (req, res, next) {
res.end('Will send all the leaders to you!');
})
.post(function (req, res, next) {
res.end('Will add the leader: ' + req.body.name + ' with details: ' + req.body.description);
})
.delete(function (req, res, next) {
res.end('Deleting all leaders');
});
leaderRouter.route('/:leaderId')
.all(function (req, res, next) {
res.writeHead(200, {'Content-Type': 'text/plain'});
next();
})
.get(function (req, res, next) {
res.end('Will send details of the leader: ' + req.params.leaderId + ' to you!');
})
.put(function (req, res, next) {
res.write('Updating the leader: ' + req.params.leaderId + '\n');
res.end('Will update the leader: ' + req.body.name +
' with details: ' + req.body.description);
})
.delete(function (req, res, next) {
res.end('Deleting leader: ' + req.params.leaderId);
})
module.exports = leaderRouter;
var express = require('express');
var bodyParser = require('body-parser');
var promoRouter = express.Router();
promoRouter.use(bodyParser.json());
promoRouter.route('/')
.all(function (req, res, next) {
res.writeHead(200, {'Content-Type': 'text/plain'});
next();
})
.get(function (req, res, next) {
res.end('Will send all the promotions to you!');
})
.post(function (req, res, next) {
res.end('Will add the promotion: ' + req.body.name + ' with details: ' + req.body.description);
})
.delete(function (req, res, next) {
res.end('Deleting all promotions');
});
promoRouter.route('/:promoId')
.all(function (req, res, next) {
res.writeHead(200, {'Content-Type': 'text/plain'});
next();
})
.get(function (req, res, next) {
res.end('Will send details of the promotion: ' + req.params.promoId + ' to you!');
})
.put(function (req, res, next) {
res.write('Updating the promotion: ' + req.params.promoId + '\n');
res.end('Will update the promotion: ' + req.body.name +
' with details: ' + req.body.description);
})
.delete(function (req, res, next) {
res.end('Deleting promotion: ' + req.params.promoId);
})
module.exports = promoRouter;
var morgan = require('morgan');
var express = require('express');
var dishRouter = require('./dishRouter')
var promoRouter = require('./promoRouter')
var leaderRouter = require('./leaderRouter')
var hostname = 'localhost';
var port = 3000;
var app = express();
app.use(morgan('dev'));
app.use('/dishes',dishRouter);
app.use('/promotions',promoRouter)
app.use('/leadership',leaderRouter)
app.use(express.static(__dirname + '/public'));
app.listen(port, hostname, function(){
//noinspection JSAnnotator
console.log(`Server running at http://${hostname}:${port}/`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment