Created
July 10, 2018 09:48
-
-
Save devarajchidambaram/aa08ae7116d426e5b8ba5997e4629149 to your computer and use it in GitHub Desktop.
Wrap express js middlewares
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Referred link https://www.lunchbadger.com/tracking-the-performance-of-express-js-routes-and-middleware/ | |
var express = require('express') | |
var uuid = require('uuid') | |
const bodyParser = require('body-parser'); | |
const { | |
EventEmitter | |
} = require('events'); | |
const profiles = new EventEmitter(); | |
var app = express() | |
function wrap(fn) { | |
return function Wrapped(req, res, next) { | |
const start = Date.now(); | |
fn(req, res, function () { | |
profiles.emit('middleware', { | |
req, | |
name: fn.name, | |
elapsedMS: Date.now() - start | |
}); | |
next.apply(this, arguments); | |
}); | |
}; | |
} | |
profiles.on('route', ({ | |
req, | |
elapsedMS | |
}) => { | |
console.log(req.method, req.url, `${elapsedMS}ms`); | |
}); | |
profiles.on('middleware', ({ | |
req, | |
name, | |
elapsedMS | |
}) => { | |
console.log(req.id, ' Middleware:', name?name:'Ananymous', `${elapsedMS}ms`); | |
}); | |
//Wrapped original app.use function here | |
var originalAppUseFn = app.use; | |
app.use = function () { | |
lastArg = arguments.length - 1; | |
if (typeof arguments[lastArg] === 'function') { | |
arguments[lastArg] = wrap(arguments[lastArg]) | |
} | |
originalAppUseFn.apply(this, arguments) | |
} | |
app.use(function (req, res, next) { | |
if (req.url === '/favicon.ico') { | |
res.send('not available'); | |
return; | |
} | |
next(); | |
}) | |
app.use(function assignId(req, res, next) { | |
const uuidv4 = require('uuid/v4'); | |
req.id = uuidv4(); | |
next(); | |
}) | |
// Make sure you register this **before** other middleware | |
app.use(function profilerMiddleware(req, res, next) { | |
const start = Date.now(); | |
// The 'finish' event will emit once the response is done sending | |
res.once('finish', () => { | |
// Emit an object that contains the original request and the elapsed time in MS | |
profiles.emit('route', { | |
req, | |
elapsedMS: Date.now() - start | |
}); | |
}); | |
next(); | |
}); | |
app.use(bodyParser.json()); | |
app.use(express.static(__dirname + '/public')); | |
app.use(function block(req, res, next) { | |
setTimeout(() => next(), 3000); | |
}); | |
//Filter by args | |
app.use('/test', function filterSpecifRoutes(req, res, next) { | |
console.log("specific routes.........", ) | |
next(); | |
}) | |
var r1 = express.Router(); | |
r1.get('*', function (req, res, next) { | |
next(); | |
}); | |
var r2 = express.Router(); | |
r2.get('*', function R2Router(req, res, next) { | |
next(); | |
}); | |
app.use(r1); | |
app.use(r2); | |
app.get("/test", function (req, res) { | |
res.send("helloworld"); | |
}) | |
app.all('*', function (req, res, next) { | |
next('unknown url') | |
}) | |
app.use(function clientErrorHandler(err, req, res, next) { | |
res.status(500).send({ | |
error: 'Something failed!' | |
}) | |
}) | |
app.listen(3000, function (err) { | |
console.log('app is listening 3000 port', err) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment