HTTP server port number - node.js

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

Related

Socket.io listening on port not working

So I have a very basic script which should only output a status code 200 when I visit my website on port 8081. But it doesn't seem to be working. Anybody knows why this is the case? I think it maybe has to do with ssl I am using, or am I forgetting something else?
//Begin config
var http = require('http');
var io = require('socket.io')(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);
I copied this from the internet to simply test if socket.io works correctly. I'm trying this on this website.
Thanks in advance!
Edit: I did run it using node script.js.
Use the http server as a variable and put the socket.io line to the end, change port:
var server = http.createServer(/*...your code...*/).listen(3000);
var io = require('socket.io')(server);
Update
I think this could be a port error, try to open the required port or check if something blocks it.
I made a little app to test Socket.IO, you can see the connection changes in the browser's console.
Live example

Why is my NodeJS server deployed to Elastic Beanstalk receiving HTTPS traffic on port 80?

I've written a very simply NodeJS server
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(80);
console.log('Server running at http://127.0.0.1:8081/');
I then deployed it to Elastic Beanstalk with the "Classic Load Balancer" option selected.
Oddly, the server responds to traffic sent with https.
I expected that it would not know how to handle this traffic since the server is only listening on port 80, which is for http (no s).
Does anyone have an explanation or at least idea of why my NodeJS is handling non-HTTPS requests?
When https request made, it is terminated at the Load Balancer. The Load Balancer then will make an http request to the listener aka the NodeJS apps. This is the default configuration of Load Balancer in ElasticBeanstalk.
So your NodeJS apps is handling an http request from the Load Balancer instead of the https directly from the client.

http.createServer in nodeJS doesnt work in company network

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!

HTTPS on same port as HTTP?

I'm writing an HTTP/HTTPS proxy server in Node.JS, I want the server to handle HTTPS traffic on the same port as HTTP.
The below image explains why I want to do this, the user can just check the box...
I realize that I can't have 2 servers listening on the same port, but is there a way to setup a main server that forwards the traffic to either HTTP or HTTPS traffic?
Something like this:
http.createServer(myHTTPCallback).listen(3129, '127.0.0.1');
https.createServer(myHTTPSCallback).listen(3130, '127.0.0.1');
//Then a main server that will listen for both kinds of traffic
main.createServer(function(req, res){
if(/*Traffic is HTTPS*/){
//Forward to 127.0.0.1:3130
}
else{
//Forward to 127.0.0.1:3129
}
}).listen(3128, '192.168.0.2');
How would I code the above?
EDIT - As per #Chris Jester-Young answer
I created this simple server, but I only receive output for HTTP requests. Also breakpoints or console logs don't trace:
var http = require('http');
http.createServer(function(req, res){
res.write('<h1>Resquest Received</h1>');
res.write('<span>Requested URL = ' + req.url + '</span>');
res.end();
}).listen(3128);
You don't have to listen to both HTTP and HTTPS on the same port, in your specific case of writing a proxy server. Browsers connect to proxy servers via HTTP, whether for proxying HTTP or HTTPS requests. HTTPS requests use the CONNECT HTTP method to open a direct connection to the HTTPS server---that CONNECT request is still done in the unencrypted portion of the proxy traffic.
Edit: Here's what I think your code should look like:
http.createServer(function (request, response) {
// Handle normal requests here
}).on('connect', function (request, socket, head) {
// Handle CONNECT requests here
}).listen(3128);

Correct way to forward traffic in nginx to node.js

I want to forward all traffic to a certain URL in nginx to node.js. I'm still new to node.js and I'm wondering if I should be using some kind of CGI service (like PHP) or if I should setup a node.js server (like nginx -> apache) and forward all traffic through nginx to that server like below:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(8124, "127.0.0.1");
This is only a single page which needs to run a node.js script. What is the best way to do this?
Nginx reverse proxy (aka proxy_pass) doesn't support keep-alive.
But it's possible to establish keep-alive connection for FastCGI using a 3rd party module, Maxim Dounin's Upstream Keepalive module.
By the way, Node.js is yet stable to run without any reverse proxy.

Resources