http.createServer in nodeJS doesnt work in company network - node.js

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!

Related

Node Js can't access to server from internet

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.

HTTP server port number

If I create and HTTP server using nodejs like this:
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8081);
// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');
does it mean that if I use a web hosting service, the URL of the website will always need to contain somehow 8081 port? How would the URL look like?
Yes you would always need to the port number with requests, however if you use relative links this will not be such a problem.
Good idea
Questionable idea
You may also look into reverse proxies and virtual hosts depending on your application.
If you expose a server using port 80 means http and / or via 443 means https. Your urls don't need port. Other than that if you are using a different port then you can use ngnix or haproxy to expose them in 80 or 443. Without following this you will end up giving port to the urls

Node.js: is it possible to simulate an HTTP connection without external modules?

Let's assum I have a basic HTTP server, like the one on nodejs.org:
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:1337/');
Is is it possible to simulate a connection to this server without using any npm module (socket.io, connect etc.) ?
The only way I have found by only using pure Node.js is by using http.request and running both the instances locally (both the server and the client simulator). However, I am unsure as to what drawbacks this has or if it is a valid option. Will this work if I try to simulate multiple connections, for example? Is network speed a bottleneck in this case? Is there a better/easier way?
Sure, you can make an HTTP request from within your computer, you won't be simulating it either, you'll be making an actual connection too. NodeJS's built in http module ships with a client as well as a server.
var http = require("http");
http.get("http://localhost:1337", function(resp){
console.log("Request made!", resp);
});

Accessing server from ip address (Node js/Windows) [duplicate]

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);

I can't access my node.js server on my AWS EC2 isntance from the outside

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

Resources