0

I am new to nodejs and still learning so please help me out here.

const http=require('http') 
const server=http.createServer((req,res)=>{
    if(req.url==='/'){
        res.end('Welcome to our home page')
    }
    if(req.url==='/about'){
        res.end("Here is our short abt page")
    }
    res.end(`
       <h1>OOps!</h1>
       <p>We can't seem to find the page you are looking for</p>
       <a href='/'>Go back home</a>
    `)
})

server.listen(2000)

The above mentioned code gives the following error:

 node:events:505
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at new NodeError (node:internal/errors:372:5)
    at ServerResponse.end (node:_http_outgoing:846:15)        
    at Server.<anonymous> (c:\Users\Hp\Desktop\node\app.js:10:9)
    at Server.emit (node:events:527:28)
    at parserOnIncoming (node:_http_server:956: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\Hp\Desktop\node> node "c:\Users\Hp\Desktop\node\app.js"
node:events:505
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at new NodeError (node:internal/errors:372:5)
    at ServerResponse.end (node:_http_outgoing:846:15)        
    at Server.<anonymous> (c:\Users\Hp\Desktop\node\app.js:10:9)
    at Server.emit (node:events:527:28)
    at parserOnIncoming (node:_http_server:956: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'
}

The page at localhost:2000 works at times for the main page (mostly) but gives the "site can't be reached" error for the rest. Do I need to start the xampp apache?

Candy
  • 53
  • 9

1 Answers1

1

Here we should observe the code execution clearly.

When we visit localhost:2000, First mentioned if condition will be executed and send the response as Welcome to our home page

 if(req.url==='/'){
    res.end('Welcome to our home page')
}

Then after it tries to check the second if condition and as the if condition is false that block of code will not be executed

 if(req.url==='/about'){
    res.end("Here is our short abt page")
}

Then it tries to execute the last block of code

res.end(`
   <h1>OOps!</h1>
   <p>We can't seem to find the page you are looking for</p>
   <a href='/'>Go back home</a>
`)

As the res is already sent with first block of if condition, Now again these last lines of code will try to send the response, which will result in an error and the app will be crashed. Because the response is already ended its process.

To overcome these issues, try to change the code in the below-mentioned way.

const server=http.createServer((req,res)=>{
if(req.url==='/'){
    res.end('Welcome to our home page')
    return
}
if(req.url==='/about'){
    res.end("Here is our short abt page")
    return;
}
res.end(`
   <h1>OOps!</h1>
   <p>We can't seem to find the page you are looking for</p>
   <a href='/'>Go back home</a>
`)
 })

The above-mentioned code snippet will work fine and fixes the issue.