Matthew Scragg Matthew Scragg - 2 years ago 83
Node.js Question

Nodejs Express implicit middleware applied to all routes?

I wanted to know if Express would let me create a route middleware that would be called by default without me explicitly placing it on the app.get() arg list?

// NodeJS newb

var data = { title: 'blah' };

// So I want to include this in every route
function a(){
return function(req, res){ = data;

app.get('/', function(req, res) {
res.render('index', { title: });

I understand I can do
app.set('data', data)
and access it via
in the route. Which would probably satisfy my simple example above.

Answer Source
function a(req, res, next){ = data;
  // Update: based on latest version of express, better use this = data;

app.get('/*', a);

See the example at the bottom of the Express docs, Middleware section.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download