用於測試目的無用中間件:Express.js控制檯中未定義的內容來自何處?
module.exports = function() {
return function rankick(req, res, next) {
if (Math.random() < 0.5) {
return next('Random kick...');
}
next();
};
};
注入簡單明確的應用程序:
var express = require('express'),
http = require('http'),
path = require('path')
rankick= require('./rankick'),
util = require('util');
var app = express();
app.set('port', process.env.PORT || 8080);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(rankick()); // Using the middleware
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.use(function (req, res, next) {
res.end('Hello World!');
});
http.createServer(app).listen(app.get('port'));
當next()
被調用,有錯誤字符串,控制檯日誌undefined
其次500
錯誤:
undefined
GET/500 28ms
GET/200 4ms
undefined
GET/500 5ms
'require('path')'位很愚蠢。它僅用於'__dirname'行之一。可能沒有必要,因爲它所做的只是添加一個'/'或'\',這取決於你的操作系統,這在生產中不會改變。只是我的2c – 2013-05-13 06:47:48