来自客户端的重复请求表达nodejs

duplicate request from the client express nodejs

本文关键字:请求 nodejs 客户端      更新时间:2023-09-26

我的应用程序有一个问题,我无法解决

首先我的app:

nodejs 0.8.14表达3.1passport-local 0.1

问题是当我登录在护照会话客户端请求页面两次…

我发现,因为我在url中放了一个请求变量

this in my router

exports.index=function(req,res)
{
    console.log('success: '+req.url);
    var sesion_usuario=validate(req.params.code_user);//if not valid return null
    if(sesion_usuario){
        res.render('logged',{title:'Hello'+sesion_usuario})

this in my browser

http://localhost:8000/YOGE7419

this in my app

app.get('/:code_user',routes.index);

这是我收到的提示

success: /YOGE7419
success: /YOGE7419
DEBUG: validate error: maxlength not match

和这个

中的url转换
http://localhost:8000/YOGE7419#sthash.zp1bOY2d.dpbs

为什么??第一次请求和第二次请求之间发生了什么?tnx

应用程序配置

app.configure(function()
{
    app.use(express.favicon(__dirname + '/public/images/favicon.png')); 
    app.set('port',  8000 || process.env.PORT);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.set('view options',{layout:false});
    //app.use(express.logger('dev'));
    app.use(express.bodyParser({uploadDir:'./public/uploads/'}));
    app.use(express.cookieParser('nomatherwhatdoyoudobatman'));
    app.use(express.session());
    app.use(passport.initialize());
    app.use(passport.session());
    //app.use(express.methodOverride());
    app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
    app.use(function(req, res, next){
        res.render('404.jade',
            {
                title: "404 - Page Not Found",
                showFullNav: false,
                status: 404,
                url: req.url
            });
    });
});

如果express重定向浏览器,那么如果重定向尚未完成,浏览器将发送重复请求(在我的经验中是在5秒后)。

通过在重定向请求中添加超时,我可以避免重复请求:

app.get('/path2', function( req, res ) {
    req.connection.setTimeout( 1000 * 60 * 10 ); // ten minutes
    console.log('path2');
});

详细讨论请阅读这里的GitHub链接。

第一个请求是由浏览器自动发出的,它请求favicon.ico,当然,第二个请求是URL(您的URL)。

进一步参考

http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/