my http module code seems to be:
const http=require(‘http’)
const server=http.createServer(function(req,res){
if(req.url===‘/’){
res.end(“welcome to the home page ,this is gopi”)
}
if(req.ulr=== ‘/about’){
res.end(‘here is our short history’)
}
res.end( <h1>Oops!</h1> <p>We can't seems to find</p> <a href="/">back to home</a> )
});
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:399:5)
at ServerResponse.end (node:_http_outgoing:985:15)
at Server. (C:\Users\Dell\OneDrive\Desktop\javascript\httpmodule.js:9:9)
at Server.emit (node:events:513:28)
at parserOnIncoming (node:_http_server:1072:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17)
Emitted ‘error’ event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:846:9)
at process.processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: ‘ERR_STREAM_WRITE_AFTER_END’
}
Node.js v18.14.0
please anyone give me response why iam getting this error
If think the problem is here.
The function does not stop executing after res.end()
In other words: res.end() !== return
const http = require("http");
const server = http.createServer(function (req, res) {
if (req.url === "/") {
res.end("welcome to the home page ,this is gopi");
}
if (req.ulr === "/about") {
res.end("here is our short history");
} else {
res.end(
`<h1>Oops!</h1> <p>We can't seems to find</p> <a href="/">back to home</a>`
);
}
});
server.listen(8080);
I did notice that if you use the localhost:5000/about as the target address it works fine. Then if you type in a non existent address like localhost:5000/other it sends you to the Oops page. Everything still OK in node.
It’s when you go to the home page localhost:5000 that node crashes and throws the error.
It only seems to be when the home page is called that the error occurs for me.