我正在尝试扩展app.get行为,但似乎在这样做之后,该应用程序丢失了我在扩展它之前所做的一些配置。
在以下代码段中,/sample和/es/sample输出为空,预期输出应为“value”
难道我做错了什么?
var app = require('express')();
app.set('myprop', 'value');
var _get = app['get'];
app['get'] = function (route, middleware, callback) {
_get.call(app, route, middleware, callback);
// For instance: I generate a new route for 'es' language.
_get.call(app, '/es' + route, middleware, callback);
};
app.get('/sample', function(req, res){
res.send(app.get('myprop'));
});
app.use(app.router);
app.listen(3000);
更新
对不起,我会回答自己...
我错过了扩展方法中的以下第一行:)
if (middleware === undefined && callback === undefined) return _get.call(app, route);
现在它就像一个魅力!
app['get'] = function (route, middleware, callback) {
if (middleware === undefined && callback === undefined) return _get.call(app, route);
_get.call(app, route, middleware, callback);
// For instance: I generate a new route for 'es' language.
_get.call(app, '/es' + route, middleware, callback);
};