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?