Ajouve
Ajouve

Reputation: 10089

exec a script on each request node.js

I'm sure I can exec a script or a function each time I exec a request (app.get or app.post) with the res and req var but I cant' find how

can you please help me

Thanks

Edit:

I try

var express = require('express')
, http = require('http')
, path = require('path');

var app = express();

app.use(function(err, req, res, next){
  console.log('aa');
  next();
});

in my app.js but I never have aa in my console

my app.get are in controllers:

files = fs.readdirSync(__dirname + '/controllers');
files.forEach(function(file){
    var name = file.replace('.js', '');
    require('./controllers/' + name)(app, models);
});

if I remove this code app.add works but I can't use app.get

Upvotes: 1

Views: 461

Answers (1)

Pickels
Pickels

Reputation: 34630

You need to use general middleware:

app.use(function (req, res, next) {
  //do stuff
  next();
});

Upvotes: 3

Related Questions