Express / Node-将标头发送到客户端后无法设置标头

时间:2019-08-02 14:03:15

标签: javascript node.js express handlebars.js

当我意识到以下错误时,我正试图制作一个中间件来拦截对我的路线“ / profile”的请求:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:464:11)
    at ServerResponse.header (/home/francisco/projetos/marketplace/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/home/francisco/projetos/marketplace/node_modules/express/lib/response.js:170:12)
    at done (/home/francisco/projetos/marketplace/node_modules/express/lib/response.js:1008:10)
    at Immediate._onImmediate (/home/francisco/projetos/marketplace/node_modules/express-handlebars/lib/utils.js:26:13)

当我使用res.send('hello')发送响应时,不会发生错误,但是使用res.render(...)时会发生错误。

我一直在寻找问题的答案,但找不到。

router.use((req, res, next) => {
    if (req.session.user) { return next() }

    res.redirect('/login')
})

router.get('/', (req, res) => {
    res.render('profile', {
        linkPartial: function () {
            return 'profile_css'
        },
        jsPartial: function () {
            return 'profile_js'
        }
    })
})

0 个答案:

没有答案