nodeJS - Choose another port than 8080

nodejs

#1

Hello,

I started a program on nodeJS with tutorial-point tutorial. It ask us to write :
.listen(8081);
at the end. it works when i launch server on my terminal with http://localhost:8081 and http://127.0.0.1.8081 but impossible on C9

The lone way i found to launch the server :
.listen(process.env.PORT, process.env.IP);
So i have two questions, why the first way works on my term but not on C9? How can i do to code exactly like the tutorial?

I tried to modify the PORT variable from env C9 terminal with export command-line but it didn’t work

thanks a lot !


#2

Localhost on c9 works same way on as on your local machine.You can check that by running server with .listen(8081); and then curl http://localhost:8081 from another terminal.
But you need to pass ‘0.0.0.0’ as ip (.listen(8081, '0.0.0.0');) to be able to access the server from other computers.


#3

It looks like the problem is different. it works with 0.0.0.0 or not. On my term when i click on the link, it sends me on an error page with the message The connection has timed out, but when i copy/paste http://localhost:8081 as URL it works.

so the problem is solved.

Thanks for the help


#4

With localhost you are accessing the server running on your local computer, server running on cloud9 can be accessed from workspace-username.c9users.io


#5

here my code

var server = http.createServer(function (request, response) {

response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello world');
}).listen(8081, '0.0.0.0');

console.log('Server running');

i did curl http://localhost:8081 and i get ‘Hello world’ on my term and when i go to the ‘real’ url : http://vast-sands-27254-mellit.c9users.io/ i have a page with “No application seems to be running here!”


#6

without port http://vast-sands-27254-mellit.c9users.io is http://vast-sands-27254-mellit.c9users.io:8080 try http://vast-sands-27254-mellit.c9users.io:8081


#7

no change, with server running and the display “hello world” from curl, http://vast-sands-27254-mellit.c9users.io:8081 display an error page: The connection has timed out


#8

curl http://vast-sands-27254-mellit.c9users.io:8081 doesn’t work because your workspace is private
opening that page in browser will work


#9

thanks a lot, it works !
Last thing but not very important. When I try to open it on a new tab from my firefox on Linux it’s impossible, but on firefox and chrome on Windows it works without problem. what could it be ?


#10

How do you open it on a new tab, are you logged in to your c9 account on that browser


#11

by clicking on the url on previous post or copy-past. The same url work on my second computer (Window).
On my centOS i’m logged in but not on my windows