Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

exec a script on each request node.js

exec a script on each request node.js

Problem

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

Problem courtesy of: Ajouve

Solution

You need to use general middleware:

app.use(function (req, res, next) {
  //do stuff
  next();
});
Solution courtesy of: Pickels

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

exec a script on each request node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×