目前我正在尝试向我的快递 http://expressjs.com/应用程序。我以为通过打电话
app.use(function (req, res, next) {
res.notice = function (msg) {
res.send([Notice] ' + msg);
}
});
通知函数将附加到我的应用程序中存在的所有 res 对象,使我能够按如下方式使用它:
app.get('something', function (req, res) {
res.notice('Test');
});
然而,上面的例子不起作用。有办法完成我想做的事情吗?
你需要打电话next
添加后notice
方法res
,并且需要在路由定义之前添加中间件。
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)