Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Last active August 29, 2015 14:18
Show Gist options
  • Save greggnakamura/f6070c3752fcbffbd98b to your computer and use it in GitHub Desktop.
Save greggnakamura/f6070c3752fcbffbd98b to your computer and use it in GitHub Desktop.
Express: Basic 'all', 'get', 'post', 'next' example with multiple callbacks (log); configuration examples
var express = require('express'),
bodyParser = require('body-parser'),
app = express();
// configurations
app.set('env', 'development'); // default: process.env.NODE_ENV
app.enable('trust proxy'); // for reverse proxy; disabled by default
app.set('jsonp callback name', 'cb'); // json with padding
app.set('json replacer', function (attr, val) {
if (attr === 'passwordHash') {
return undefined;
}
return val;
});
app.enable('case sensitive routing'); // disabled by default: /hello /Hello
app.enable('strict routing'); // enabled by default: /hello /hello/
app.enable('view cache'); // recommended in production environment
// view configurations
app.set('view engine', 'jade') // or 'ejs'; eliminates need for extensions in `render`
function log (req, res, next) {
console.log(names);
next();
}
app.use(bodyParser.urlencoded());
var names = [];
app.all('/', function (req, res, next) {
console.log('from ALL');
next();
});
app.get('/', log, function (req, res) {
res.render('index.jade', { names: names });
});
app.post('/', function (req, res) {
names.push(req.body.name);
res.redirect('/');
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment