'ERR_STREAM_WRITE_AFTER_END'

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> )
});

server.listen(8080);
iam getting error was:
PS C:\Users\Dell\OneDrive\Desktop\javascript> node httpmodule.js
node:events:491
throw er; // Unhandled ‘error’ event
^

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

I’m also watching the same tutorial and with the same error, still not found any solution except this - javascript - Error [ERR_STREAM_WRITE_AFTER_END]: write after end - Stack Overflow

What if you try

res.write("content");
res.end();

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);

None of the solutions worked for me either.

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.

How does your code look like?

// server.js
const http = require("http")
const server = http.createServer((req, res) => {
    if (req.url === "/") {
        res.end("Index page.")
    }
    else if (req.url === "/about") {
        res.end("About page.")
    } else {
        res.end("Ooops. Page not found.")
    }

})
server.listen(8080, "localhost", () => console.log("Server is listening..."));

When I run this code, it is running as expected.

> node server.js
image

localhost:8080
image

localhost:8080/about
image

localhost:8080/somepage
image

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 .

Here is the compete code after an hour of debugging. Node Version matters. Mine is V18.16.1 LTS.

"use strict"
const PRINT = console.log

// http module - setting up a web server

import http from "http"

const respond = {
    hostname: "127.0.0.1",
    port: 3000,
    statusOK: 200,
    contentType: "Content-Type",
    textType: "text/plain",
}

const httpd = http.createServer((req, res) => {
    if (req.url === "/") {
        PRINT(`URL Requested: ${req.url}`)
        res.statusCode = respond.statusOK
        res.setHeader(respond.contentType, respond.textType)
        res.end("Welcome to homepage.")
    }
    if (req.url === "/about") {
        PRINT(`URL Requested: ${req.url}`)
        res.statusCode = respond.statusOK
        res.setHeader(respond.contentType, respond.textType)
        res.end("Welcome to the 'aboutpage'")
    }
    if (req.url === "/err"){
        PRINT(`URL Requested: ${req.url}`)
        res.statusCode = respond.statusOK
        res.setHeader(respond.contentType, respond.textType)
        res.end("This is the error page")
    }
})
httpd.listen(respond.port, respond.hostname, () => {
    PRINT(`Server is running at http://${respond.hostname}:${respond.port}`)
})

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);

[FIXED] Here is the only solution you will need.

// Starts

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.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.