Logging middleware
Express comes with a logger middleware via Connect; it's very useful for debugging an Express application. Let's add it to our Express server ./lib/express/index.js
:
var express = require('express')
, http = require('http')
, config = require('../configuration')
, heartbeat = require('../routes/heartbeat')
, notFound = require('../middleware/notFound')
, app = express();
app.set('port', config.get('express:port'));
app.use(express.logger({ immediate: true, format: 'dev' }));
app.get('/heartbeat', heartbeat.index);
app.use(notFound.index);
http.createServer(app).listen(app.get('port'));
module.exports = app;
The immediate
option will write a log line on request instead of on response. The dev
option provides concise output colored by the response status. The logger middleware is placed high in the Express stack in order to log all requests.