1

I am new to NodeJS and learning, I want to know how to make an sample app using NodeJS. I have tried it on localhost and it is working. Now I am trying to host it public to any server, but it is not working.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1/');

This is code is saved in my local machine as m.js, I run:

$ node m.js

It runs fine and when i open it in browser as https://127.0.0.1:1337

I am getting the output as :

Hello World

I want the same thing to do from a remote server i.e. I have a domain www.example.com and if I open it with https://www.example.com:1337 then it should show on my browser screen like previously :

Hello World

But it is not working.

3 Answers 3

1

@Annanta 127.0.0.1:1337 is a local machine ip address. please remove this. Please find below the sample code. Try to access it with remote server ipaddress. Please note the remote machine must have node and npm installed.

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(1337);

console.log('Server running');

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , but when i am trying to request example.com:1337 , it is not working
1

Here is a possible work-around using an Apache2-Webserver:

Just edit the Virtual Host in your conf.d (for Ubuntu you´ll find it in /etc/apache/), run a2enmod proxy and restart the Apache.

Here is a possible configuration:

NameVirtualHost *:80
<VirtualHost *:80>
     ServerName your-domain.com
     ServerAlias www.your-domain.com
     ProxyRequests off
     ProxyPass / http://127.0.0.1:1337/
     ProxyPassReverse / http:/127.0.0.1:1337/
</VirtualHost>

5 Comments

Thanks , but i am using centos, can you give me correct configuration for it
You´ll find the conf.d in /etc/httpd/. The configuration is the same like when using Ubuntu.
Make sure you are running your node application on the configured port.
@ananta Can you change the port to 80 or 8080 and verify if it is working or not.
how to configure a port for nodejs
1

Its actually really simple. Just don't use any IP at all and just define the port.

var http = require('http');
http.createServer(function (req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
}).listen(1337);

console.log('Server running!');

Also as Sach stated you can't just upload it to a webserver. You have to have node and npm installed and you have to actually run the code via

$ nodejs application.js

5 Comments

Thanks , but when i am trying to request example.com:1337 , it is not working
The exact code I gave you is working just fine for me. Keep in mind this is only working when the server is running Here is a screenshot of my PuTTY input if you are interested
I agree , in my putty it is also working, showing the same as in pic u have attached. but when i am trying to access it friom browser using example.com:1337 , it is showing web page is not available
Try using the IP of the webserver it's my only idea to be honest :/
No idea then tbh. sry :/