Understanding Express.js tutorial - whose local host am I pointing to and how?

Hello, all. I am missing a subtle nuance in this tutorial and come to you pleading for direction.

The tutorial gives this code:

// Require what we need
var http = require(“http”);

// Build the server
var app = http.createServer(function(request, response) {
  response.writeHead(200, {
    "Content-Type": "text/plain"
  });
  response.end("Hello world!\n");
});

// Start that server, baby
app.listen(1337, "localhost");
console.log("Server running at http://localhost:1337/");

then goes on to say:

"…if you run that app (if that file is called app.js, you’d run node app.js), you’ll get a response of “Hello world!” if you visit localhost:1337 in your browser. "

I have run the code and - boom - “Server running at http://localhost:1337/

Nice!

Now I want to hit that page and see that “Hello World!” message.

First thing I did was open a browser window on my own machine…but that did not work.

"This site can’t be reached

localhost refused to connect."

I have tried opening http://localhost:1337/ and https://localhost:1337/ on the c9.io browser, and am still not reaching the site.

What am I doing wrong?

If you’re on c9, you can only use ports 8080, 8081 and 8082. You can’t just use localhost to see the running app. It goes something like https://<workspace name>-<account name>.c9users.io (there should be a Preview button in c9. Click that, then click View Running Application (or something similar))

2 Likes

your code works fine i copied it pasted it into notepad saved it as app.js put it in a folder i created and then ran it using node.js … and it will run on localhost:1337 … if you have node installed on your computer you can also do this … just cd to folder then type node app … but if you are new to this i understand why you are confused … as previous person stated about ports to be used on c9 … i havent used c9 yet so i cant say … you could try changing app.listen(1337, “localhost”); to
app.listen(8080, “localhost”); should work.

1 Like

Getting there…

Followed your lead and now I’ve got the following open in the C9!

running
https://learnnode-olddognewtrix123.c9users.io/localhost:8080/

However I am getting the No application seems to be running here! message. I’ll keep tinkering to get ti to work.

You don’t include localhost:8080 at the end.
learnnode-olddognewtrix123.c9users.io is already the host

But I still see nothing :confused:
Is the server running?

From the C9 browser that is currently pointed at https://learnnode-olddognewtrix123.c9users.io/

"Cloud9 can’t get you to your requested workspace. Here are some suggestions on how to figure out what’s going on:

Check that the workspace name (learnnode) and username (olddognewtrix123) are typed correctly." — CHECKS OUT
"Check that the server is successfully running on Cloud9:" I MEAN, “Server running at http://localhost:8080/” — SO, IT’S RUNNING
"If the server hit an error, the output window will have a message telling you what it is" — NO ERRORS REPORTED
"If you’re in the middle of debugging code, your server might be paused right now" — NOT RUNNING DEBUG MODE
"The server might be running on a different port; make sure it’s on port $PORT with $IP as the IP address" — I AM RUNNING “app.listen(8082, “localhost”);” already (I TRIED 8080, 8081, and now 8082)

So, not sure where to go from here. Still not seeing “Hello World!”

Hi, in C9 I use:

var port = process.env.PORT;

and:

app.listen(port);

edit:
if you want to use “hostname” this should work:

var port = process.env.PORT;
var localhost = process.env.IP;
app.listen(port, localhost);

Cheers and happy coding :slight_smile:

2 Likes

In your app.listen(), try removing the 'localhost' string. Just pass 8080.

I tried your code in my instance of c9, but without that 'localhost'. I still got the little error toast in the terminal, but I could see the running app.

Holy moly that worked!!!

var http = require("http");
var port = process.env.PORT;    // added this

var app = http.createServer(function(request, response) {
  response.writeHead(200, {
    "Content-Type": "text/plain"
  });
  response.end("Hello world!\n");
});

//app.listen(8082, "localhost");   --- commented this out
app.listen(port);     // added this
console.log("Server running at http://localhost:8082/");

Got server running, then clicked Tools → Preview → Preview Running Application

C9 opened a window to:

https://learnnode-olddognewtrix123.c9users.io/

And …

    "Hello world!" 

                   appeared in the browser window

Thanks!!!

1 Like

Hi @olddognewtrix123, That console.log() is hard-coded.

  • It’s means that every time is executed will print the same message, no matter what the real port number is.

You can try this:

console.log("Server running at http://localhost:1337/  " + port);

The second number is the port number.

  • Is not given information about the server: because is “outside”.

  • you need a callback:

// Start that server, baby
app.listen(port, function() {
  console.log("Server running at port number: " + port );    
});

Cheers and happy coding :slight_smile:

1 Like