Travis Brown
Node.js developer, and DevOps engineer at emeals.com
Presented by Travis Brown
$ npm install express
var express = require('express'); var app = express();
app.get('/', function(req, res){ res.send('Hello World!'); });
app.listen(3000);
$ curl localhost:3000
Hello World!
var express = require('express'); var app = express();
app.post('/book', function(req, res){ // Connect to a data source, create a book, assign to variable book res.json(201, book); }); app.get('/book/:id', function(req, res){ // Connect to a data source, read a book by id, assign to variable book res.json(200, book); }); app.put('/book/:id', function(req, res){ // Connect to a data source, update a book by id, assign to variable book res.json(200, book); }); app.delete('/book/:id', function(req, res){ // Connect to a data source, delete a book by id res.send(204); });
app.listen(3000);
This works, but is not sustainable.
var express = require('express'); var book = require('./book.js'); var app = express();
app.post('/book', book.create); app.get('/book/:id', book.fetch); app.put('/book/:id', book.update); app.delete('/book/:id', book.destroy);
app.listen(3000);
Much better, but still cumbersome for multiple services.
{
"resources": [
{
"name" "book",
"methods": "./book.js"
},
{
"name": "author",
"methods": "./author.js"
}
]
}
exports.create = function(req, res){
// Connect to data source, create a book, assign to variable book
res.json(201, book);
};
exports.fetch = function(req, res){
// Connect to data source, read a book by id, assign to variable book
res.json(200, book);
};
exports.update = function(req, res){
// Connect to data source, update a book by id, assign to variable book
res.json(200, book);
};
exports.destroy = function(req, res){
// Connect to data source, delete a book by id
res.send(204);
};
var fs = require('fs');
var express = require('express');
var config = require('config');
var auth = require('./auth.js');
var app = express();
app.use(express.json());
app.use(express.urlencoded());
services = fs.readdirSync(config.serviceDir);
services.forEach(function(service) {
var versions = fs.readdirSync(config.serviceDir+"/"+service);
versions.forEach(function(version) {
var serviceConfig = JSON.parse(fs.readFileSync(config.serviceDir+"/"+service+"/"+version+"/"+config.serviceFile))
serviceConfig.resources.forEach(function(resource) {
resource.path = "/"+service+"/"+version+"/"+resource.name;
app.post(resource.path, auth.authenticate, auth.authorize, resource.methods.create);
app.get(resource.path + '/:id', auth.authenticate, auth.authorize, resource.methods.fetch);
app.put(resource.path + '/:id', auth.authenticate, auth.authorize, resource.methods.update);
app.delete(resource.path + '/:id', auth.authenticate, auth.authorize, resource.methods.destroy);
})
})
});
var server = app.listen(config.net.port, function() {
console.log('Listening on port %d', server.address().port);
});
By Travis Brown
This presentation gives a brief overview to the concepts of REST and Express, and describes the structure used at eMeals to deploy services as modules.