NodeJS重定向到有效路由引发错误,NodeError:无效状态代码



My Node应用程序需要设置特定的session令牌/字段来执行设置。

这个方法和其他authentication类型的方法的问题是,在我的路由器文件之外的任何重定向似乎都会因为同样的原因而失败。

示例代码(mcve((您需要为此设置快速会话(

  1. 创建文件auth.js

  2. 添加功能并导出:

    const checkAdminAuthenticatedSetup = () => (req, res, next) => {
    if(req.session.authenticated !== true) {
    return res.status(302).redirect('/setup/auth', {message: "Please enter password to continue setup"})
    } else {
    next();
    } 
    }
    module.exports = {
    checkAdminAuthenticatedSetup
    }
    
  3. 在路由器文件中,const auth = require("auth")

  4. 对于某些路线,请执行以下操作:

    router.get('/financial',
    auth.checkAdminAuthenticatedSetup(),        <---------- this line should check if there is a req.session.authenticated (= true) value, else redirect to another page /setup/auth
    function (req, res, next) {
    res.render("setup-banking-information");
    });
    

问题

该重定向";失败";带有以下消息:

>>> console.log(err)
NodeError: Invalid status code: [object Object] at ServerResponse.writeHead (_http_server.js:209:11) at ServerResponse.writeHead (C:UsersCybeXWebstormProjectsawesome-appnode_moduleson-headersindex.js:44:26) at ServerResponse.writeHead (C:UsersCybeXWebstormProjectsawesome-appnode_moduleson-headersindex.js:44:26) at ServerResponse._implicitHeader (_http_server.js:200:8) at write_ (_http_outgoing.js:585:9) at ServerResponse.end (_http_outgoing.js:702:5) at ServerResponse.end (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpress-sessionindex.js:355:19) at ServerResponse.redirect (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibresponse.js:947:10) at C:UsersCybeXWebstormProjectsawesome-apputilsauth.js:30:32 at Layer.handle [as handle_request] (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterlayer.js:95:5) at next (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterroute.js:137:13) at Route.dispatch (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterroute.js:112:3) at Layer.handle [as handle_request] (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterlayer.js:95:5) at C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterindex.js:281:22 at Function.process_params (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterindex.js:335:12) at next (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterindex.js:275:10) at Function.handle (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterindex.js:174:3) at router (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterindex.js:47:12) at Layer.handle [as handle_request] (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterlayer.js:95:5) at trim_prefix (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterindex.js:317:13) at C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterindex.js:284:7 at Function.process_params (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibrouterindex.js:335:12)
stack = "RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: [object Object]n at ServerResponse.writeHead (_http_server.js:209:11)n at ServerResponse.writeHead (C:UsersCybeXWebstormProjectsawesome-appnode_moduleson-headersindex.js:44:26)n at ServerResponse.writeHead (C:UsersCybeXWebstormProjectsawesome-appnode_moduleson-headersindex.js:44:26)n at ServerResponse._implicitHeader (_http_server.js:200:8)n at write_ (_http_outgoing.js:585:9)n at ServerResponse.end (_http_outgoing.js:702:5)n at ServerResponse.end (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpress-sessionindex.js:355:19)n at ServerResponse.redirect (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibresponse.js:947:10)n at C:UsersCybeXWebstormProjectsawesome-apputilsauth.js:30:32n at Layer.handle [as handle_request] (C:UsersCybeXWebstormProjectsawesome-appnode_modulesexpresslibro"… (length: 2,501)
message = "Invalid status code: [object Object]"
__proto__ = RangeError {constructor: , name: , code: }
[[StableObjectId]] = 3

每次发生这种重定向(在路由器文件之外(,这种方法(在app.js内部(都会命中

// error handler
app.use((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');
});

此外,我注意到的是;渲染";error.pug页面,浏览器只是挂在它最初尝试加载的页面上,而不在浏览器中呈现error页面,浏览器就是永远加载。

询问并回答。这只是一个简单的疏忽

Express不允许通过重定向方法解析附加信息,例如(在我的情况下(用于呈现页面{ message: "..."}的附加参数

redirect()的格式/语法为:

res.redirect([status,] path)

在我的情况下,我应该将此添加为一条快闪消息。

相关内容

  • 没有找到相关文章

最新更新