Cannot Connection to Port at Simple Node Js server - node.js

I am stumped.
I have a node js server running on a subdomain.
Every time i try to access it from a browser i get ERR_CONNECTION_REFUSED error.
Here is my server code
const http = require('http');
http.createServer( function (request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124, "my-server-ip");
console.log('Server running at http://127.0.0.1:8124/');
Additional Information:
I am running a VPS with Hostgator.
The subdomain shares an IP with main domain; something like 412.x.xx.xxx .

insert console log inside create server function. Then you can identify error for this. Also you can use error response.
const http = require('http');
http.createServer( function (request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
console.log('Server running at http://127.0.0.1:8124/');
}).listen(8124, "my-server-ip");

Related

Does starting a server at localhost:8081 creates internet connection issues?

I have started a server at local host:8081 as
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/');
After starting the service, I am not able to access internet. please help me with this.

getting server forbidden error in my browser

I have just started exploring node.js. Have installed msi file on my windows server. My code below returns me expected output in command window
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/`');
But when i type http://127.0.0.1:8081/ in my browser i dont get any output. When i see console i get below error
Failed to load resource: the server responded with a status of 403 (Forbidden)
What i wrong and how to fix? I am following this link
probably like my current PC, your's too must be running McAfee or some other program is already using port 8081, you got two options:
stop McAfee or the other program running on that port
listen to a different port on your node server
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(300);
console.log('Server running at http://127.0.0.1:300/');
I changed it to 300 from 8081 and it worked.
http://127.0.0.1:300/ this url gives the desired output.

Node.js example gives 2 responses?

I have made an example script from Node.js website main page like so:
var http = require('http');
http.createServer(function (req, res) {
console.log("We are connected");
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/');
Any ideas why each time I connect to 127.0.0.1:1337 (via chromium) I get 2 responses saying "We are connected"??
There are two requests by the browser. One for url / and another for /favicon.ico. Try to output the request url.
console.log(req.url);

Node.js htaccess like get rules

I am looking for a way to set dynamic url http://host.com/dynamic_value-123/ and be able to get these values (123)
Considering you have the most basic example of a server with Node.js, you can achieve that with req.url.split('-')[1]:
var http = require('http');
http.createServer(function (req, res) {
console.log(req.url.split('-')[1]);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');

Remote IP from request on http server

is there any way to know the remote IP adress form a request on on the http server?
Using "net" for a socket connection is socket.remoteAddress but for the http server I get undifined for the same .remoteAddress
Regards
I'm just adding this because I came across this article and it still took me a little bit of time to figure out it was as you see in the following:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello ' + req.connection.remoteAddress + '!');
console.log("request received from: " + req.connection.remoteAddress);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
You can use the request.connection method to retrieve the net.Socket object, where you can then use your socket.remoteAddress property.

Resources