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.
Yes it should work , because if we do no put the oops content in an else block it will still run even after the code has executed the above if blocks.
This will lead to node executing an “end” code for the response even after the response has ended for it in one of the above blocks , which eventually leads to the error. Hope this explaination helps anyone who gets stuck in the future .
Hi as far as I know the following error means that you are writing
response after sending it through res.end(), so there is work-around through use of if-elseif-else and res.write().
const http= require("http");
const server= http.createServer((req, res)=>{
if(req.url == "/"){
res.write("Welcome to our Home Page");
//res.end("Welcome to our page");
}
else if(req.url == "/about"){
res.write("Little about us");
// res.end("little about us");
}
else{
res.write(`<h1> HMM.. </h1>
<p>We couldn't find what u are looking for </p>
<a href= "/">go Back to home </a>`)
//res.end("404");
}
res.end();
})
server.listen(3000);
const http = require('http');
const server = http.createServer((req,res)=>{
if(req.url === '/'){
res.end('Welcome to our home page')
return
}
if(req.url === '/about'){
res.end('Our short history')
return
}
res.end(`
<h1>Oops!</h1>
<p>We can't seem to find the page you are looking for!</p>
<a href="/">back home</a>
`);
})
server.listen(5000);
// End
I have added an extra return statement before each if statement ends which makes sure that with every request, The response is made and then it is not continued after that.