2

I was trying to make a middleware to intercept the requisitions to my route '/profile', when I realized the error below:

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)

When I send the response with res.send('hello'), the error doesn't happen but when using res.render(...) it happens.

I was looking for a response for my problem and I couldn't find it.

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'
        }
    })
})
x80486
  • 6,627
  • 5
  • 52
  • 111

0 Answers0