我试图在node.js服务器上调试我的代码,突然发现一些非常奇怪的东西,每个导航器请求都会执行21次app.use回调函数,这是我见过的最奇怪的事情。
现在,我是节点的新手,这是常见的吗?你会问我怎么注意到这一点,我基本上设置了一个名为request
的0初始化的计数器变量,并且每次执行回调时我都将它增加1并将其打印在地址栏中以检查当前值,每当我访问localhost时,计数器增加21意味着有21个连续的回调......有什么想法?
//libraries
var app = require('express')(),
http = require('http').Server(app),
express = require('express'),
server = require('http').createServer(app),
port = process.env.PORT || 3000,
path = require('path'),
logger = require('morgan'),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
//custom
var databases = require('../common/config/databases'),
reactivity = require('./reactivity/init'),
requestValidator = require('./middleware/validateRequest.js'),
apiConfig = require('./routes/api.js');
//ROUTING
app.all('/api/v1/*', [requestValidator]);//check /api/v1/* requests for an authenthic token
app.use('/', apiConfig);
var request = 0;
app.use(function (req, res, next) {//404
var err = new Error('Not Found');
err.status = 404;
request += 1;
res.redirect('#404' + request);
next();
});
app.set('port', 4000);
var server = app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + server.address().port);
});