Skip to content

Instantly share code, notes, and snippets.

@lfender6445
Last active September 22, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfender6445/50477f49c91433c822e7ed82fc9c8bac to your computer and use it in GitHub Desktop.
Save lfender6445/50477f49c91433c822e7ed82fc9c8bac to your computer and use it in GitHub Desktop.
cannot-set-headers-after-they-are-sent-to-the-client.js
// exmaple.js
var express = require('express')
var app = express()
app.get('/foo', function (req, res) {
res.redirect(301, '/bar')
res.send('hello world') // should not set body while redirect in flight
})
app.listen(3000)
// › node example.js
// Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
// at ServerResponse.setHeader (_http_outgoing.js:485:11)
// at ServerResponse.header (/Users/lfender/source/ag.js/node_modules/express/lib/response.js:771:10)
// at ServerResponse.send (/Users/lfender/source/ag.js/node_modules/express/lib/response.js:170:12)
// at /Users/lfender/source/ag.js/example.js:6:7
// at Layer.handle [as handle_request] (/Users/lfender/source/ag.js/node_modules/express/lib/router/layer.js:95:5)
// at next (/Users/lfender/source/ag.js/node_modules/express/lib/router/route.js:137:13)
// at Route.dispatch (/Users/lfender/source/ag.js/node_modules/express/lib/router/route.js:112:3)
// at Layer.handle [as handle_request] (/Users/lfender/source/ag.js/node_modules/express/lib/router/layer.js:95:5)
// at /Users/lfender/source/ag.js/node_modules/express/lib/router/index.js:281:22
// at Function.process_params (/Users/lfender/source/ag.js/node_modules/express/lib/router/index.js:335:12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment