This question already has answers here:
Node.JS Not working on the internet
(3 answers)
Closed 9 years ago.
I have a js file with the following code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hellosldksldksldk World\n');
}).listen(3000, '127.0.0.1');
console.log('Server running');
Now, if I access the server by 127.0.0.1:3000 its perfectly fine, but I want to access it from my own computer ip address. I write 192.xxx.x.xxx:3000, but I cant access it. Since I am developing an android application I need the ip address of the computer in order to run it, can someone explain why I am unable to access it?
When you say listen(3000, '127.0.0.1'), you're explicitly binding your server to port 3000 on IP 127.0.0.1.
You probably just want to bind to all IPs, which you can do by omitting the bind host:
listen(3000);
Related
i am trying to url in node.js project
currently show in my local server port like -http://localhost:1337
expected url =http://localhost:1337/nodeprovider
var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(port);
If I understood you right: you need your node.js server to response on http://abcwebsite
/nodeprovider
Since this that task must be done not in Node.js server I suggest you to read about domain parking: https://en.wikipedia.org/wiki/Domain_parking
Making long thing short: Node.js server is not responsible for domain name it responds on. Inside server script you can only set port to listen.
From an old answer
You dont assign a domain to a node.js server, instead you load your app onto a machine which has an ip adress. You then need to make sure your app is listening on the correct port, which on most servers is 80.
now getting a domain name to point to this ip adress is an entirely separate matter. You need to make your name server point to the ip. Your name server will usually be the company you bought the domain name through, for instance GoDaddy is a Domain Name Server (DNS). So if you had a domain name with them, you would go on their site under DNS settings and change the ip adress. Your domain name will then point to your ip adress and should render your node.js app.
So I have two questions about the topic. I have purchased a VPS with digitalocean.com to host my node.js app and they gave me an example hello world app to start.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var html = "hello world";
res.end(html);
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
So this code works locally and when I host on the VPS I replace 'localhost' with the IP address of my VPS and it is available on the web when I go to http://IP_Address_of_VPS:8080
My first question: How do I host it live on the web but have it point to my IP address? When I replace 'localhost' with my IP address it does not work when I go to http://my_IP_Address:8080
My second question:
I made a test app following a node.js tutorial and here is the code
var PORT = process.env.PORT || 8080;
var express = require('express');
var app = express();
var http = require('http').Server(app);
app.use(express.static(__dirname + '/public'));
http.listen(PORT, function(){
console.log('listening on port *:'+PORT);
});
Where public is the folder with a simple html page. This app works when I run it locally at http://localhost:8080
Can someone explain how the second app runs locally when I did not specify 'localhost' in the http.listen() function? Also how do I point the second app to run from my IP address if the process is any different from the first?
First question
I hope I understood your question right, here is an attempt at an answer :)
Depends a bit on what you mean with 'my ip address'. If it is your public ip adress, you might have to set up port forwarding. Most routers will deny traffic from the outside network (which you are doing when connecting to your own public ip address) to the internal network by default.
If you mean a private ip address (local to the local network) you need to you use your public ip address and set up port fowarding.
When you set up port forwarding to your local machine you might also want to make your private ip address static.
However, I do not recommend using your home computer as a production server. You will need to solve a lot of problems like making your network secure and having low downtime.
Second question
When no hostname is passed to http.listen, the server will accept connections on all adresses. See documentation for more information.
This works, because when you omit arguments in a function call, they will default toundefined. For example, http.listen(8080) might appear as http.listen(8080, undefined, undefined, undefined). Inside the function body these will often be substituted with some default value.
The process of making your app available to the rest of the world should not be any different.
I am a beginner programmer so my technical skills are low in general.
The following code:
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(1337, '127.0.0.1');
Runs without a problem on my home network.
But doesnt run on my Business Network.
Code in Atom:
Headers I get back after running in the browser:
If it helps, we use a Windows server 2012 for the network and I have access in it.
Is this really related to the server?
As Yerken mentioned, your URL should be http://localhost:1337, and not http://localhost/1337.
In your code, you are listening to port 1337, but by default, the browser tries to access port 80 if no port is specified. That means localhost/1337 is trying to access port 80, and not 1337 which you are listening to.
So change the URL to :1337 as oposed to /1337 and it should work :)
Happy programming!
hi first of all I have limited knowledge of network.
operating system is mac, firewall is turned off.
it works on a local network: http://192.168.1.2:8080 (add to safari)
next I got my ip address from google.com "what is my ip"
78.157.xx.xxx, so I tried from internet connect to server like:
http://78.157.xx.xxx:8080 (add to safari), but with no success :(
I have a router and 3 devices, and all these three devices have the same ip 78.157.xx.xxx?
server.js
var http = require('http');
http.createServer(function (req, res)
{
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
})
.listen(8080, "0.0.0.0");
You need an open port on your router to allow a connection in to your home network from the Internet. Then you need to create a port forward from 78.157.xx.xxx:8080 to 192.168.1.2:8080.
You can get more details about port forwarding here.
Here is a useful guide and resource to configure the router.
I'm trying to run a basic node.js server,
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('hello world!\n');
}).listen(3000, '0.0.0.0', function() {
console.log('Server running on port 3000');
});
However when I run it and go to http://x.x.x.x:3000/ the page doesn't load.
I tried the answer on this question but that didn't work either. And changing the host to 127.0.0.1 or the server ip or emitting it doesn't fix it either.
I've also followed this guide that says to proxy requests with haproxy. But that did not work either.
Is there something in the security tab I have to enable/disable?
Edit: The problem was I was using the wrong IP. The IP changes when the instance is restarted.
Create a rule to open port 3000 in the security group associated with your ec2 instance.
It can be done through the command line tools or through the web console, which is more straightforward. If you didn't specify a security group when creating the instance it will be the "default" security group.
A decent walkthrough for the console
Amazon documentation
Rightscale explanation of different firewall situations