USING A PROCESS MANAGER
Easy clustering and scaling without altering application
code
StrongLoop Process Manager (strong-pm)
PM2
Comparison Chart
Forever
STRONGLOOP AND NGINX
If you're using strong-pm you can use the
!
StrongLoop nginx
controller
~$ npm install g strongnginxcontroller
~$ slnginxctlinstall
Install the Controller on the load balancing host...
ORDER MATTERS!
Middleware are executed in the order specified
app.use( express.logger('dev') );
app.use( myAuthModule() );
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
// Routing middleware...
MIDDLEWARE - WHEN DOES IT END?
Middleware processing ends when next() is not called
(or an error is generated)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/about', function aboutUs(req, res, next) {
res.send('We are StrongLoop!');
// no call to next()
});
app.get('/user', ...);
CUSTOM MIDDLEWARE - ERRORS
app.use(function (req, res, next) {
// do something...
if (thereWasAnError) {
var err = new Error(' ... ');
next( err );
return;
}
// No error, so we proceed...
next();
});
TEMPLATES
Small blocks that we can plug data into at run-time
// /views/index.jade
doctype html
html
head
title #{title}
body
section.mainbody.clear
#{homepageText}
THE 4.0 ROUTER INTERFACE
// in routes/users.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
// Get a list of users...
res.render('user/list', { results: users });
});
router.get('/:id', function(req, res, next) {
// Get a single user...
res.render('user/myaccount', { user: user });
});
router.post('/', function(req, res, next) {
// Create a user...
res.redirect('user/myaccount', { user: user });
});
module.exports = router;
THE 4.0 ROUTER INTERFACE
// in app.js
var express = require('express'),
...;
var app = express();
// app config and middleware...
app.use('/users', require('./routes/users'));
RESPONSE METHODS
response.send(data) or response.end(data)
response.status(httpStatus)
response.send(201, someData)
response.sendfile('path/to/someFile.json')
response.download('/report-12345.pdf')
HTTP STATUS CODES
2XX: for successfully processed requests
3XX: for redirections or cache information
4XX: for client-side errors
5XX: for server-side errors