Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
Why nodejs is not allowing me to access other route when I access the root route?

I am using the HTTP module of nodejs to create three routes('/','/about' and the last one treats any other route that is not defined as error route). When I access the root route first and try to access other route nodejs throw an error but when I access the error route or the about the route and try accessing another route it works fine.

Below are the code I wrote and the error nodejs throw

Error

PS C:\Users\Maxwell\Desktop\node> node app.js
node:events:368
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.end (node:_http_outgoing:846:15)
    at Server.<anonymous> (C:\Users\Maxwell\Desktop\node\app.js:10:9)
    at Server.emit (node:events:390:28)
    at parserOnIncoming (node:_http_server:951:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
    at emitErrorNt (node:_http_outgoing:726:9)
    at processTicksAndRejections (node:internal/process/task_queues:84:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}
PS C:\Users\Maxwell\Desktop\node> node app.js
node:events:368
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.end (node:_http_outgoing:846:15)
    at Server.<anonymous> (C:\Users\Maxwell\Desktop\node\app.js:10:9)
    at Server.emit (node:events:390:28)
    at parserOnIncoming (node:_http_server:951:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
    at emitErrorNt (node:_http_outgoing:726:9)
    at processTicksAndRejections (node:internal/process/task_queues:84:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}

Code

const http = require('http');

const server = http.createServer((req,res)=>{
    if(req.url==='/'){
    res.end('Welcome Home Dev, You are loved');
    }
    if(req.url==='/about'){
        res.end('This is the about page')
    }
    res.end(
`<h1>OOOp</h1>
<p>It seen like this page does not exit</p>

<a href='/'>back to homepage</a>

`
    );
});

server.listen(5000);

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Change to an if/else so you're only processing one branch of the if per request:

const server = http.createServer((req,res)=>{
    if(req.url === '/'){
        res.end('Welcome Home Dev, You are loved');
    } else if (req.url === '/about') {
        res.end('This is the about page')
    } else {
        res.end(
`<h1>OOOp</h1>
<p>It seen like this page does not exit</p>

<a href='/'>back to homepage</a>`);
    }
});

Or, alternately, you could add a return after each res.send() to stop further execution in your request handler after you send a response. Remember, that just because you call res.send() your function still continues to execute so you need to manage control flow so the other code that sends a response doesn't execute once you've already sent a response.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda