express中 请求 过一段时间就会出现请求等待 没有响应
发布于 7 年前 作者 xdayannanfei 3643 次浏览 来自 问答

最近在开发一个微信公众号的项目,用的express,以前没学过nodejs.连续在5分钟内点浏览器中点 http://云服务器IP/ 或http://云服务器IP/?a=1&b=2&c=3 最后怎是出现等待服务器响应,要按ctrl+C 才能在node.exe控制台程序中将日志打印出来,然后又能正常响应了。 app.get(appconfig.wechart.verifytokenURLpath, verifytoken.verify); 就这,响应微信认证。 下面是app.js代码 ’use strict’;

var express = require(‘express’); var path = require(‘path’); var fs = require(‘fs’); var logger = require(‘morgan’); var fileStreamRotator = require(‘file-stream-rotator’); var weixinutils = require(’./lib/weixin_utils’); var cookieParser = require(‘cookie-parser’); var bodyParser = require(‘body-parser’); var timeout = require(‘connect-timeout’); //express v4 var port = process.env.PORT || 3000; var app = express();

var verifytoken = require(’./lib/verifytoken.js’); var appconfig = require(’./config/config.json’);

console.log(__dirname); console.log(path.join(__dirname, ‘views/index.html’)); app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘jade’);

app.listen(port);

console.log('app started port ’ + port); var logDirectory = path.join(__dirname, ‘log’); fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory); //var accessLogStream = fs.createWriteStream(path.join(__dirname, ‘log/access.log’), {flags:‘a’}); var accessLogStream = fileStreamRotator.getStream({ date_format: ‘YYYYMMDD’, filename: path.join(logDirectory, ‘access-%DATE%.log’), frequency: ‘daily’, verbose: false })

app.use(timeout(‘10s’)); app.use(logger(‘dev’)); //app.use(haltOnTimedout); //app.use(logger(‘dev’, {stream: accessLogStream})); app.use(bodyParser.json()); //app.use(haltOnTimedout); app.use(bodyParser.urlencoded({ extended: false })); //app.use(haltOnTimedout); app.use(cookieParser()); //app.use(haltOnTimedout); app.use(’/weixinpay’, express.static(path.join(__dirname, ‘public’))); //app.use(haltOnTimedout);

app.use((req, res, next) => { console.log(’----------------------------------------------------’); console.log(‘req.method:’, req.method); console.log(‘req.originalUrl:’, req.originalUrl); console.log(’----------------------------------------------------’); next(); }); app.use(haltOnTimedout);

app.get(appconfig.wechart.verifytokenURLpath, verifytoken.verify); app.use(haltOnTimedout);

function haltOnTimedout(req, res, next){ console.log(‘haltOnTimedout state:’); console.log(req.timedout); if (!req.timedout) { next(); } }

app.use(function (req, res, next) { console.log(‘404 not found.’); res.status(404).send(‘Sorry cant find that!’); });

app.use(function (err, req, res, next) { console.log(‘error error error.’); console.error(err.stack); res.status(500).send(‘Something broke!’); });

/*

// catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error(‘Not Found’); err.status = 404; next(err); });

// error handler app.use(function(err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get(‘env’) === ‘development’ ? err : {};

// render the error page
res.status(err.status || 500);
//res.render('error');
res.send('error');

}); */

verifytoken.js代码.

‘use strict’;

var appconfig = require(’…/config/config.json’); var crypto = require(‘crypto’);

function verify(req, res) { console.log(’–req.url—’, req.url); console.log(’—req.query—’, req.query); var tmpstr = [appconfig.wechart.token, req.query.timestamp, req.query.nonce].sort().join(’’); var sha1 = crypto.createHash(‘sha1’); sha1.update(tmpstr); var _signature = sha1.digest(‘hex’); if (_signature === req.query.signature) {

        res.send(req.query.echostr);
        console.log('认证通过');



} else {
    res.send('signature error.');
    console.log('认证失败');
}

}

module.exports.verify = verify;

2 回复

有高手在不???

好像用pm2启动程序就没有这个现象,直接用node 运行就有这个现象,哪位大虾讲讲区别。暂时自己解决。

回到顶部