Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
¿Por qué nodejs no me permite acceder a otra ruta cuando accedo a la ruta raíz?

Estoy usando el módulo HTTP de nodejs para crear tres rutas ('/', '/about' y la última trata cualquier otra ruta que no esté definida como ruta de error). Cuando accedo a la ruta raíz primero e intento acceder a otros nodos de ruta, aparece un error, pero cuando accedo a la ruta de error o sobre la ruta e intento acceder a otra ruta, funciona bien.

A continuación se muestra el código que escribí y el 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' }

Código

 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 answers
Answer question

0

Cambie a if/else para que solo esté procesando una rama del if por solicitud:

 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>`); } });

O, alternativamente, puede agregar un return después de cada res.send() para detener la ejecución adicional en su controlador de solicitudes después de enviar una respuesta. Recuerde que solo porque llama a res.send() , su función aún continúa ejecutándose, por lo que debe administrar el flujo de control para que el otro código que envía una respuesta no se ejecute una vez que ya haya enviado una respuesta.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!