nodejs请求没有明显原因锁定

时间:2017-06-14 19:55:48

标签: node.js request timeout

我正在开发和测试使用搜索栏查找数据库记录的应用程序。

我遇到了一个奇怪的问题,我可以执行多个搜索,所有这些搜索都会正确地返回结果,然后我的应用程序将锁定没有明显的原因,并且在我可以执行任何其他操作之前需要大约120秒才能超时请求。我记录每个请求,似乎没有任何明显的锁定原因,除了它似乎是某种超时。我可以输入它锁定的完全相同的查询字符串,它将正确执行,但在几次搜索后它将再次锁定。

我有错误处理程序登录到控制台并且没有报告错误,实际上它会在超时后返回200响应标头。我最后一次测试它说请求花了36毫秒,但我知道看时钟已经过了120秒。有没有办法诊断可能导致问题的原因?当它锁定时,任何新的请求也将被冻结,直到请求超时。

我现在无法发布任何代码片段,因为它的一部分是敏感的,但我可以发布我怀疑可能是其中一个问题的依赖项列表:

"bcrypt-nodejs": "latest",
"body-parser": "~1.0.0",
"connect-flash": "~0.1.1",
"cookie-parser": "~1.0.0",
"ejs": "latest",
"express": "~4.0.0",
"express-mysql-session": "^1.2.0",
"connect-mongo" : "latest",
"express-session": "~1.0.0",
"method-override": "~1.0.0",
"morgan": "~1.0.0",
"mssql": "latest",
"mysql": "latest",
"mongoose" : "latest",
"passport": "~0.1.17",
"passport-local": "~0.1.6",
"request": "^2.79.0",
"request-promise": "^4.1.1",
"socket.io": "latest"

我在Windows 8.1环境中运行Node v6.9.2。请求通过POST发送到路由处理程序,该处理程序查询mssql数据库并将格式化的JSON回复返回给客户端。 如果需要任何其他信息,请告诉我。 谢谢, -Tim

----------------------------SERVER.JS-----------------------------------
const port      = 3002
var express     = require('express')
var app         = express()                           
var server      = require('http').Server(app)
var io          = require('socket.io')(server)
cookieParser  	= require('cookie-parser')
var bodyParser  = require('body-parser')
// SESSION
session       = require('express-session')
MongoStore    = require('connect-mongo')(session)
var passport  = require('passport')
var flash     = require('connect-flash')

// DEV
var morgan    = require('morgan')

// DATABASE
configDB	= require('./config/database.js')
mongoose    = require('mongoose')
mssql       = require('mssql')
mysql       = require('mysql')

// CONFIG
require('./config/passport')(passport);
app.use(morgan('dev')); 
app.use(express.static(__dirname + '/public'))
app.use(cookieParser()); 
app.use(bodyParser()); 
app.set('view engine', 'ejs'); 
mongoose.Promise = global.Promise
const mongoConnect = mongoose.createConnection(configDB.mongoConnectString)
app.use(session({
  secret: '*******',
  store: new MongoStore({mongooseConnection: mongoConnect})
}))  
app.use(passport.initialize());
app.use(passport.session()); 
app.use(flash()); 
// CACHE CONTROL
app.use(function(req, res, next) {
    if (req.user) { res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate') }
    next()
})
// ROUTES
require('./app/routes.js')(app, passport);
//ERROR HANDLER
app.use(function(err, req, res, next){
  console.log("ErrorHandler: " + err.stack)
})
//Start Server
server.listen(port);
console.log('App launched on port '  + port);


--------------------------------- Routes.js ----------------------------------------------
module.exports = function(app, passport){                                                          
	app.get('/', function(req, res){
		if(req.user){
		  return res.redirect('/profile')
		}
		else {
		  res.render('home.ejs', {dest_page: req.query.dest_page, html_content: '<h2>Welcome! Please Login</h2>', main_content: 'login',  loggedState: 'Login', loginError: req.flash('loginMessage') })    
		}
	})
	app.post('/lookup', isLoggedIn, function(req, res){
		var lookup = require('../app/models/lookup')
		switch(req.query.command) {           
		  case "search_results" : 
			finishRequest = function(err, records){ res.render('client_lookup', { errorMessage: err, client: records, user : req.user}) }
			lookup.client(req.body.s)
			break
		  default :
			res.render('home.ejs', {errorMessage: 'Error: nothing to show', user : req.user})
			break
		}
	  }) 
   }
  function isLoggedIn(req, res, next){
    if(req.isAuthenticated())
      return next()
    res.redirect('/?dest_page=' + req.url)
  }
  ------------------------------- Lookup.js -------------------------------------------
function Lookup() {}
Lookup.client = function(s){
    var query
	if(typeof s == 'undefined'){
      finishRequest("Search string not set", null)
    }
    s = s.trim()
    if(s.length <= 2){
      finishRequest("At least 2 characters required", null) 
    }
    // Match Date
	if(s.match(/^\d\d[-.\/]\d\d[-.\/](\d\d|\d\d\d\d)/)){
		query = '(date match query)'
	}
	// Match Phone
	else if (s.match(/^[()0-9]+/)){
		query = '(phone match query)'
	}
	// Match Name
	else if(s.match(/^[a-z]/)){
		query = '(name match query)'
	}
	else {
		finishRequest('Unrecognized Pattern!', null)
	}
	mssql.connect(configDB.mssql).then(function(){
		new mssql.Request().query(query).then(function(records){
		  if(records.length > 0)
			finishRequest(null, records)
		  else 
			finishRequest("Nothing Found!", null)
		}).catch(function(err){
			finishRequest(err, null)
		})
	})                     
}
module.exports = Lookup

0 个答案:

没有答案