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

"cant set headers after they are sent" occured in 2.23.2 but not in 2.21.0 #1481

Closed
jiadi0801 opened this issue Jan 4, 2018 · 1 comment
Closed

Comments

@jiadi0801
Copy link

I use browser-sync as the server in my project since v2.18.13. Yesterday, I reinstalled my project. When I run it, the cli print:

_http_outgoing.js:494
    throw new Error('Can\'t set headers after they are sent.');

env

  • Browsersync [ 2.23.2 and 2.21.0 ]
  • Node [ 8.9.3 ]
  • windows 7
  • Git Bash

test case

// const browserSync = require('../bs/node_modules/browser-sync/');   // for bs 2.23.2
const browserSync = require('../bs21/node_modules/browser-sync/');  // for bs 2.21.0

let bs = browserSync.create('bsServer');
bs.init({
	server: '',
    open: true,
    middleware: [mw]
});
function mw(request, response, next) {
    response.write('bs');
    response.end();
    next(); 
}

when use v2.23.2, the server will stop, and throw the error below.
v2.21.0 works fine.

_http_outgoing.js:494
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:494:11)
    at ServerResponse.setHeader (_http_outgoing.js:501:3)
    at D:\TMP\bs\node_modules\serve-index\index.js:213:13
    at D:\TMP\bs\node_modules\serve-index\index.js:324:7
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

I cannot call response.end or response.write method in my middleware.
If I have to call these methods, I have to remove the next() method. This seems to prevent the middleware flow.

I want to know how this problem arises, thanks!

@shakyShane
Copy link
Contributor

I've pushed a fix that will work for you when doing the example above

Note: It will still not work if you do something like server: 'app' as this will append a server-static middleware to the end of the stack and will result in the same bug

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

2 participants