Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_HTTP_HEADERS_SENT #2

Open
xiopt opened this issue Jan 8, 2019 · 0 comments
Open

ERR_HTTP_HEADERS_SENT #2

xiopt opened this issue Jan 8, 2019 · 0 comments

Comments

@xiopt
Copy link

xiopt commented Jan 8, 2019

Hello,
I am using your mvc solution and I get the [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client error sometimes.
Here is an example:

0|server  | Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
0|server  |     at ServerResponse.setHeader (_http_outgoing.js:470:11)
0|server  |     at ServerResponse.header (/var/www/scrapper-node/node_modules/express/lib/response.js:767:10)
0|server  |     at ServerResponse.send (/var/www/scrapper-node/node_modules/express/lib/response.js:170:12)
0|server  |     at done (/var/www/scrapper-node/node_modules/express/lib/response.js:1004:10)
0|server  |     at tryHandleCache (/var/www/scrapper-node/node_modules/ejs/lib/ejs.js:257:5)
0|server  |     at View.exports.renderFile [as engine] (/var/www/scrapper-node/node_modules/ejs/lib/ejs.js:482:10)
0|server  |     at View.render (/var/www/scrapper-node/node_modules/express/lib/view.js:135:8)
0|server  |     at tryRender (/var/www/scrapper-node/node_modules/express/lib/application.js:640:10)
0|server  |     at Function.render (/var/www/scrapper-node/node_modules/express/lib/application.js:592:3)
0|server  |     at ServerResponse.render (/var/www/scrapper-node/node_modules/express/lib/response.js:1008:7)
0|server  |     at ServerResponse.res.render (/var/www/scrapper-node/node_modules/express-flash/lib/express-flash.js:29:16)
0|server  |     at ServerResponse.res.render (/var/www/scrapper-node/app/kernel.js:129:20)
0|server  |     at index (/var/www/scrapper-node/app/controllers/VideoController.js:13:28)
0|server  |     at process._tickCallback (internal/process/next_tick.js:68:7)

VideoController.js looks like this:

module.exports = {
    /**
     * @param  {Object} req
     * @param  {Object} res
     * @param  {Function} next
     */
    index: async(req, res, next) => {
        const id = req.params.id
        if(typeof id === 'undefined') return res.notFound('Invalid video id')
        try {
            const cached = await CacheService.get(id)
            if (cached) {
                return res.render('video', JSON.parse(cached)) ### // Here is line 13
            }
            else {
                const video = await Video.findById(id)
                if (video === null) return res.notFound('Video not found')

                const links = VideoStreamService.create(req, res, id, video)

                if (links.status === 500) {
                    return res.notFound('No active sources found')
                }
                CacheService.set(id, JSON.stringify({
                    links: links.data
                }))
                return res.render('video', {
                    links: links.data
                })
            }
        }
        catch (err) {
            // next(err)
            return res.badRequest('Temporary error. Try again later or choose another mirror.')
        }

    },
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant