Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

737
Vistas
Error [ERR_STREAM_WRITE_AFTER_END]: write after end, http server node.js

Setting up a new http server with Node.js

When you first launch it, it displays the proper html text on the screen but when you move to other links in my code ie: localhost:5001/about, it gives me an error in my IDE console

events.js:377
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at new NodeError (internal/errors.js:322:7)
    at writeAfterEnd (_http_outgoing.js:694:15)
    at ServerResponse.end (_http_outgoing.js:815:7)
    at Server.<anonymous> (/Users/angelo/Documents/coding/node/node-tutorial/app.js:11:7)
    at Server.emit (events.js:400:28)
    at parserOnIncoming (_http_server.js:900:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:127:17)
Emitted 'error' event on ServerResponse instance at:
    at writeAfterEndNT (_http_outgoing.js:753:7)
    at processTicksAndRejections (internal/process/task_queues.js:83:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}
[nodemon] app crashed - waiting for file changes before starting...

app.js

const http = require('http');

const PORT = 5001;
const server = http.createServer((req, res) => {
  if(req.url === '/'){
    res.end('Home Page')
  }
  if(req.url === '/about'){
    res.end('About Page')
  }
  res.end('Error page')
})

server.listen(PORT, () => {
  console.log(`Server is listening on port: ${PORT}`)
})

I've been looking for answer for the 'write after end' error but have not seen this specific issue for this specific block of code

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As mentioned on the comments, this is due to res.end being called multiple times, the simplest solution for you would either to return after each res.end or adjust your conditions:

const http = require('http');

const PORT = 5001;
const server = http.createServer((req, res) => {
  if(req.url === '/'){
    res.end('Home Page')
  }
  else if(req.url === '/about'){
    res.end('About Page')
  }
  else {
    res.end('Error page')
  }
})

server.listen(PORT, () => {
  console.log(`Server is listening on port: ${PORT}`)
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda