Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
NodeJS Can only connect when Server is shutdown

Im trying out setting up a webserver with nodeJS with this code:

const http = require("http")
const port = 8080
const fs = require("fs")

const server = http.createServer(function (req, res) {
    res.writeHead(200, { "Content-Type": "text/html" })
    fs.readFile("index.html", function (error, data) {
        if (error) {
            res.writeHead(404)
            res.write("Error")
        } else {
            res.write(data)
        }
        res.end
        
    })
})

server.listen(port, function (error) {
    if (error) {
        console.log("Something went wrong")
    } else {
        console.log("Server is listening on port " + port)
    }
})

But when I connect on any browser(tried chrome and mozilla) using either localhost or 127.0.0.1 the page will only show when I shut the server down. It's constantly loading, not showing anything until I shutdown with ctrl + C. Then it will show my HTML page. Same problem was when I didnt use an html page and just responded with

res.write("hey")
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

res.end is a function, try res.end()

You had never ended the request so the browser has been waiting for it to end and it only happened when you closed the server.

about 4 years ago · Santiago Trujillo Report

0

Because you just show the method

res.end

but not run it.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!