1

I write following in nodejs and run it. but can not connect to localhost:8080. I am use http and express module.

const http=require('http');
const PORT=process.env.PORT || 8080;
const express=require('express');
const app=express();

http.createServer((req,res)=>{
    res.end('Hellow world')
}).listen(PORT);

or

app.listen(PORT);
app.use('/',(req,res)=>{
    res.send("Hellow world");
});
2
  • what is your error or problem, cause it worked right. Commented Mar 5, 2022 at 21:38
  • It loading. But dont show content. Commented Mar 6, 2022 at 17:25

1 Answer 1

0

When I run it on my pc it works fine.

It could be that:

  • You are not running it from your pc, so it's not running on localhost.
  • The link is not the correct one. The correct one is http://localhost:8080
  • Try adding "localhost" beside PORT to change it to:
http.createServer((req,res)=>{
    res.end('Hellow world')
}).listen(PORT, "localhost");

Could you put a picture of the exact error or screen you're seeing?

2
  • It is loading but does not show any content. Commented Mar 6, 2022 at 18:10
  • what does the chrome console say Commented Mar 8, 2022 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.