http-party / http-server

a simple zero-configuration command-line http server
MIT License
13.68k stars 1.5k forks source link

Cannot set headers after they are sent to the client #805

Open chrisspen opened 2 years ago

chrisspen commented 2 years ago

Server is unable to send any requests.

Environment Versions

  1. OS Type = Ubuntu 20.04
  2. Node version: $ node --version = v10.24.1
  3. http-server version: $ http-server --version = 14.1.0

Steps to reproduce

#!/bin/bash
set -e
mkdir example
cd example
echo '{}' > package.json
npm install --save-dev webpack webpack-cli typescript ts-loader http-server
npx tsc --init

cat <<EOF > tsconfig.json
{
  "compilerOptions": {
    "target": "es2020",
    "module": "es2020",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}
EOF

cat <<EOF > webpack.config.js
module.exports = {
  entry: "./src/index.ts",
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
  },
  output: {
    filename: "bundle.js",
  },
  devServer: {
    publicPath: "/dist",
  },
};
EOF

cat <<EOF > index.html
<!DOCTYPE html>
<title>Demo</title>
<script src="./dist/bundle.js"></script>
<div>Hello World!</div>
EOF

mkdir src
cat <<EOF > src/index.ts

async function load() {
  document.body.textContent = JSON.stringify({123:'abc'});
}

load();
EOF

./node_modules/.bin/webpack --mode=development
./node_modules/.bin/http-server

Expected result

The file index.html is served.

Actual result

Upon making a request to http-server running at http://127.0.0.1:8080, it crashes with the error:

[Thu Apr 07 2022 22:45:19 GMT-0400 (Eastern Daylight Time)]  "GET /" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
_http_outgoing.js:470
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at module.exports.ResponseStream.(anonymous function) [as setHeader] (/project/sqljs/node_modules/union/lib/response-stream.js:100:34)
    at Object.exports.(anonymous function) (/project/sqljs/node_modules/http-server/lib/core/status-handlers.js:57:7)
    at Readable.stream.on (/project/sqljs/node_modules/http-server/lib/core/index.js:339:22)
    at Readable.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
zbynek commented 2 years ago

Duplicate of https://github.com/http-party/http-server/issues/756 -- Node 10 is not supported, please try Node 12.16 or later

github-actions[bot] commented 2 years ago

This issue has been inactive for 180 days