Can't set headers after they're sent
See original GitHub issueNote: for support questions, please use one of these channels: stackoverflow or slack
You want to:
- report a bug
- request a feature
Current behaviour
I am trying to make an app with Node.js, Express and Socket.io. I’m using EJS as the view engine. This is the relevant part of my code -
const express = require('express');
const app = express();
//copied from socket.io tutorial
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '../templates'));
...
app.get('/', function (req, res) {
logger.info('index');
return res.render('index');
});
...
const port = process.env.PORT || localConfig.port;
http.listen(port);
Now if I navigate to localhost:3000, I get the error
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at ServerResponse.header (/home/aniket/Documents/HumbleHelper/node_modules/express/lib/response.js:767:10)
at ServerResponse.contentType (/home/aniket/Documents/HumbleHelper/node_modules/express/lib/response.js:595:15)
at ServerResponse.send (/home/aniket/Documents/HumbleHelper/node_modules/express/lib/response.js:145:14)
at done (/home/aniket/Documents/HumbleHelper/node_modules/express/lib/response.js:1004:10)
at tryHandleCache (/home/aniket/Documents/HumbleHelper/node_modules/ejs/lib/ejs.js:228:10)
at View.exports.renderFile [as engine] (/home/aniket/Documents/HumbleHelper/node_modules/ejs/lib/ejs.js:437:10)
at View.render (/home/aniket/Documents/HumbleHelper/node_modules/express/lib/view.js:135:8)
at tryRender (/home/aniket/Documents/HumbleHelper/node_modules/express/lib/application.js:640:10)
From the log, I can see that ‘index’ has been called twice, followed by the error. However, if I remove this line -
const io = require('socket.io')(http);
Steps to reproduce (if the current behaviour is a bug)
Navigate the browser to localhost:3000 and it fails. I have tried a res.send()
instead of res.render()
but that fails too.
Expected behaviour
The index page should be rendered correctly
Setup
- OS: Arch Linux
- browser: Google Chrome 59
- socket.io version: 2.0.4
Other information (e.g. stacktraces, related issues, suggestions how to fix)
I’m also using Passport, Helmet, Body-parser and express-session in conjunction with Socket.io
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top Results From Across the Web
Error: Can't set headers after they are sent to the client
The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function...
Read more >Cannot set headers after they are sent to the client | End Your If
Why am I receiving the error message can't set headers after they are sent to the client Nodejs? · Calling the request handler...
Read more >Cannot set headers after they are sent to the client in JS
The "Cannot set headers after they are sent to the client" error occurs when the server in an express.js application sends more than...
Read more >Resolving HTTP Cannot set headers after they are sent ... - Fjolt
This is quite a confusing error if you aren't familiar with HTTP headers. This error arises when you send more than 1 responses...
Read more >Understanding Node Error [ERR_HTTP_HEADERS_SENT]
This post describes why the Node Error [ERR_HTTP_HEADERS_SENT] cannot set headers after they are sent.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This happens if I share the same port between Socket.io and Express
Thanks @rwalle61 for your help! As far as I remember I was following the default Node JS template for IBM Bluemix (It was for a hackathon), and the lines were already there if memory serves! Anyway, I completed the hackathon by removing those lines. Now I see why this happened! Thanks for your help!