Presented by Chris Manson (@real_ate)
April 30, 2014 - NodeUpNorth Leeds
Here are a few of my tips and best practices on how to write and structure your NodeJS code
— Chris Manson (@real_ate) April 30, 2014 - Alternate Universe
This talk now:
Don't write any code! Sit back, relax and let me do that for you
— Chris Manson (@real_ate) April 30, 2014
"Convention over Configuration" is another way of saying "Make Easy Things Easy"
— Yehuda Katz (@wycats) July 2, 2011
$ 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!
//# myapp/app.js
var express = require('express');
var app = express();
//Some inline routes
app.get('/me', function(req, res){
res.json(req.session.user) //simplified
});
//Routes from other files
app.get('/analytics', require('./routes/analytics.js').trackEvent);
//Load a collection of routes from another file - IOC
require('./routes/verify.js')(app);
//Load a whole bunch of ungrouped routes
require('./routes').init(app);
$ npm install express-autoroute
var express = require('express');
var app = express();
var autoroute = require('express-autoroute');
autoroute(app);
app.listen(3000);
//# routes/hello.js
module.exports.autoroute = {
get: {
'/hello' : function(req, res){
res.send("Hello World!");
}
}
}
$ curl localhost:3000/hello
Hello World!
//# routes/hello.js
module.exports.autoroute = {
get: {
'/hello' : function(req, res){
res.send("Hello World!");
},
'/hello/:type' : function(req, res){
res.send("Hello " + req.params.type + "!");
}
}
}
$ curl localhost:3000/hello
Hello World!
$ curl localhost:3000/hello/Everybody
Hello Everybody!
//# routes/api/goodbye.js
module.exports.autoroute = {
get: {
'/goodbye' : function(req, res){
res.send("Goodbye cruel World!");
},
'/goodbye/:type' : function(req, res){
res.send("Goodbye " + req.params.type + "!");
}
}
}
$ curl localhost:3000/api/goodbye
Goodbye cruel World!
$ curl localhost:3000/api/goodbye/Everybody
Goodbye Everybody!
Action | HTTP Verb | URL |
---|---|---|
Find | GET | /people/123 |
Find All | GET | /people |
Update | PUT | /people/123 |
Create | POST | /people |
Delete | DELETE | /people/123 |
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);
Much clearer , but still cumbersome for multiple REST resources.#// routes/book.js var express = require('express'); var app = express(); module.exports.autoroute = { get: { '/book': function(req, res){ res.json(201, book); }, '/book/:id': function(req, res){ res.json(200, book); } }, put: { '/book/:id': function(req, res){ res.json(200, book); } }, delete: { '/book/:id': function(req, res){ res.send(204); } } }
app.listen(3000);
//# routes/api/blacklists.js module.exports.autoroute = { get: { '/blacklists' : [authentication, authorisation, get_all], '/blacklists/:id' : [authentication, authorisation, get] }, post: { '/blacklists/:id' : [authentication, authorisation, update], '/blacklists' : [authentication, authorisation, create] }, delete: { '/blacklists/:id' : [authentication, authorisation, del] } };
function get_all(req, res){ //do something } ...
$ npm install express-autoroute express-autoroute-json
var express = require('express');
var app = express();
var autoroute = require('express-autoroute');
autoroute(app);
app.listen(3000);
//# routes/book.js
var autorouteJson = require('express-autoroute-json');
var Chats = require('../db/models/Chats');
module.exports.autoroute = autorouteJson({
model: Chats, //mongoose model object
find: {}
})