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

498
Views
Mensaje de "Error no controlado" al configurar el servidor a través del módulo http en el nodo js

He estado escribiendo el siguiente código para configurar un servidor básico a través de node.js. Mi código -

 const http = require('http') const server = http.createServer((req, res) => { // console.log(req) if(req.url === '/'){ res.end("Welcome to our home page") } if(req.url === '/aboutus'){ res.end("Welcome to about us page") } res.end(` <h1> OOPS !!! </h1> <p>You have requested for any wrong address </p> <a href = "/">Get back to HOME Page</a> `) }) server.listen(5000)

Pero este código mientras se ejecuta está dando el siguiente error en mi consola. Error --

 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> (/home/tirtharaj/MERN/Node/tutorial(fcc)/8-Built-in Modules/6-httpModule(brief).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' }

No puedo encontrar ningún error en mi código. Cualquier actualización en el navegador web da error. Así que por favor ayúdame a ejecutar este código.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Esto resolverá su problema, necesita tener todo el código envuelto en declaraciones if else para que siempre pueda terminar con éxito con solo una ruta posible a través del código.

 const http = require("http"); const server = http.createServer((req, res) => { if (req.url === "/") { res.end("Welcome to our home page"); } else if (req.url === "/aboutus") { res.end("Welcome to about us page"); } else { res.end(` <h1> OOPS !!! </h1> <p>You have requested for any wrong address </p> <a href = "/">Get back to HOME Page</a> `); } }); server.listen(5000);

Personalmente, usaría una declaración de cambio de caso para mantenerlo limpio y ordenado.

 const http = require("http"); const server = http.createServer((req, res) => { switch (req.url) { case "/": res.end("Welcome to our home page"); break; case "/aboutus": res.end("Welcome to about us page"); break; default: res.end(` <h1> OOPS !!! </h1> <p>You have requested for any wrong address </p> <a href = "/">Get back to HOME Page</a> `); break; } }); server.listen(5000);
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!