Skip to content

Instantly share code, notes, and snippets.

@mallim
Created September 6, 2013 11:41
Show Gist options
  • Save mallim/6462693 to your computer and use it in GitHub Desktop.
Save mallim/6462693 to your computer and use it in GitHub Desktop.
Code sample to run express (with Handlebars as the view engine)
var express = require('express');
var cons = require('consolidate');
var http = require('http');
var reload = require('reload');
var app = express();
var server = http.createServer(app);
// Assign the handlebars engine to .html files
app.engine('html', cons.handlebars);
// set .html as the default extension
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
// New call to compress content
app.use(express.compress());
// Serving up the content from public directory
app.use(express.static(__dirname + '/public'));
// Automatically parse JSON in POST requests
app.use(express.bodyParser());
// Set the sessions
app.use(express.cookieParser());
app.use(express.session({secret: '1234567890QWERTY'}));
// Dump errors
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
app.get('/example/:id', function(req, res) {
// Perform the operation for the GET
});
app.post('/example',function(req, res) {
// Perform the operation for the POST
});
// Reload code here
reload(server, app);
// Starts listening on port 3000
server.listen(3000);
console.log("Express server listening on port %d", server.address().port );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment