extend expressjs res property

Currenty I am trying to add an error & notice function to my expressjs app. I thought that by calling

app.use(function (req, res, next) {
  res.notice = function (msg) {
    res.send([Notice] ' + msg);
  }
});

the notice function would be attached to all res objects present in my application, enabling me to use it as follows:

app.get('something', function (req, res) {
  res.notice('Test');
});

However, the example above does not work. Is there a way to accomplish what I'm trying to do?

You need to call next after adding notice method to res.

app.use(function (req, res, next) {
  res.notice = function (msg) {
     res.send('[Notice] ' + msg);
  }
  next();
});

And you need to add this middleware before routes definition.

UPDATE:

You need to add your middleware before router.

var express = require('express');
var app = express();

app.use(function (req, res, next) {
    res.notice = function (msg) {
        res.send('[Notice] ' + msg);
    };
    next();
});

app.use(app.router);
app.get('/', function (req, res) {
    res.notice('Test');
});

app.listen(3000);