Reputation: 3
this is my code :
const fs = require("fs");
// creat the server
const server = http.createServer((req, res) => {
console.log(req.url, req.method);
res.write("hello out there nice try");
res.end();
fs.readFile('./index.html', (err, data) =>{
if(err){
console.log(err);
res.end();
} else{
res.write(data);
res.end();
}})
});
server.listen(3000, 'localhost', () =>{
console.log("the server is running");
});
whenever i run it i got this error:
/ GET
events.js:352
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_http_outgoing.js:694:15)
at write_ (_http_outgoing.js:706:5)
at ServerResponse.write (_http_outgoing.js:687:15)
at /Users/khadija/Desktop/first-project/app.js:15:17
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:73:3)
Emitted 'error' event on ServerResponse instance at:
at writeAfterEndNT (_http_outgoing.js:753:7)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
the server is running but it doesn't read the index.html file does it have something to do with asynchronous?
Upvotes: 0
Views: 9462
Reputation: 1
I faced the same issue
const http = require('http');
const server = http.createServer((req,res)=>{
if(req.url==='/'){
res.end('Welcome to Home Page');
}
if(req.url==='/about'){
res.end('Here is the short History about our company')
}
else{
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(8080);
Emitted 'error' event on ServerResponse instance at: at emitErrorNt (node:_http_outgoing:857:9) at process.processTicksAndRejections (node:internal/process/task_queues:83:21) { code: 'ERR_STREAM_WRITE_AFTER_END' }
What I did to solve this was to change the if-else condition, it seemed that I made the mistake by using res.end() multiple times without properly checking the if-else condition.
Solution:
const http = require('http');
const server = http.createServer((req,res)=>{
if(req.url==='/'){
res.end('Welcome to Home Page');
}
else if(req.url==='/about'){
res.end('Here is the short History about our company')
}
else{
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(8080);
Now it works perfectly.
Upvotes: 0
Reputation: 219
You can't write to the response stream after you have already ended it.
Here:
res.write("hello out there nice try");
res.end();
You appear to want a separate conditional branch. Where you write 'nice try' if some condition is meet and write the contents of index.html otherwise.
So you should do that instead:
if (something) {
res.write("hello out there nice try");
res.end();
} else {
fs.readFile('./index.html', (err, data) =>{
if(err){
console.log(err);
res.end();
} else{
res.write(data);
res.end();
}});
}
There is also fs.readFileSync
if that is less confusing for you. Though there should not be any issue with the way that you have it as long as you don't end the stream prematurely.
Upvotes: 1