ERR_CONNECTION_REFUSED from socket.io - node.js

I'm trying to run Node.js application. With SSH I am able to run 'node server.js':
var express = require('express');
var app = express();
var server = app.listen(5431);
app.use(express.static('public'));
console.log("### RUNNING ###");
var socket = require('socket.io');
var io = socket(server);
and it indeed logs and throws no errors. But when I open client-browser app, I get following output in console:
Failed to load resource: net::ERR_CONNECTION_REFUSED
GET http://localhost:5431/socket.io/?EIO=3&transport=polling&t=MWCsv9T net::ERR_CONNECTION_REFUSED
Client connects with:
var socket = io.connect('http://localhost:5431');
I've tried connecting both with IP and domain, with same result. App worked fine on local machine.
I've checked open ports with following php script:
for($i=0; $i<10000; $i++) {
portCheck($i);
}
function portCheck($port) {
if(stest('127.0.0.1', $port))
echo $port . "<br>";
}
function stest($ip, $portt) {
$fp = #fsockopen($ip, $portt, $errno, $errstr, 0.1);
if (!$fp) {
return false;
} else {
fclose($fp);
return true;
}
}
and I did get:
As it listed 5431 I'm assuming that port is indeed open.
I have no idea then what can cause this error.

var socket = io.connect('http://localhost:5431');
This means the client will connect to the local machine - where local is from the perspective of the system where script is executing. Since the script is executing in the browser it means that your application is expected to run on the clients machine where the browser is running.
Failed to load resource: net::ERR_CONNECTION_REFUSED
GET http://localhost:5431/socket.io/?EIO=3&transport=polling&t=MWCsv9T net::ERR_CONNECTION_REFUSED
While you don't explicitly say this a later statement (see below) suggests that this is done on a different machine than your application is running on. If your application is running on host A and your browser on host B then localhost inside the client browser (where script gets executed) refers to host B and not host A.
App worked fine on local machine
This is expected since in this case localhost is the machine where your application is actually running on (same machine for application and browser).

Related

When running script for express web server with node on windows get prompt to select app

I'm going through this this course currently.
There is a tutorial on how to configure and run express server. The following script is suggested:
var express = require('express');
var path = require('path');
var open = require('open');
var port = 3000;
var app = express();
app.get('/',function(req, res){
res.sendFile(path.join(__dirname,'../src/index.html'));
});
app.listen(port,function(err){
if(err){
console.log(err);
}
else{
open('localhost:'+port);
}
});
When I run it in project root with $ node buildScripts/srcServer.js I get a system prompt You'll need a new app to open this localhost with the only suggestion being look at windows store.
What's that about? What does it need an app for? In the course when the script is run the browser is opened, but it's on Mac there. When I navigate manually to localhost:3000 there is the error like there is supposed to be, but I'm somewhat concerned that this behavior will mess with live reloading so I'd like to get rid of it.
Add the http:// prefix and it will open using the default browser.
open('http://localhost:'+port);

Http Serving with node.js over the web

This is a very basic question. But I have looked and can't seem to find any tutorials that walk through this step. Everything either stops just before this step OR starts just after it.
I have launched an AWS server (Windows_Server-2016-English-Full-Containers-2016.10.18 (ami-d08edfc7)) and installed node in the default directory.
I have put a file into the following content:
var http = require('http');
const PORT=8080;
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
var server = http.createServer(handleRequest);
server.listen(PORT, function(){
console.log("Server listening on: http://localhost:%s", PORT);
});
I then open CMD, navigage into the directory where node is installed and run the program with:
node myServer.js
Next I open a browser and navigate to http://localhost:8080 and I am served some content. Terrific.
My question is how do I go about making a request of that newly installed server from another machine over the internet. My primitive guess was to simply navigate to the AWS machine's public IP, as displayed in the AWS console and include the port number.
So for example if my IP were 55.173.140.15 I would type in the address http://55.173.140.15:8080 and expect to see the page. That is not working. So what configuration step am I missing?

Can't get os-service app working except from command line

I have a node.js app that I want to run as a windows service, and I'm using os-service to try to achieve that.
The service installs seemingly correctly, and the Windows Services management console provides this info about it:
In the listing:
Name: wibble
Description: (blank)
Status: (blank)
Startup Type: automatic
Log On As: Local System
In the properties/General pane:
Path to executable:
"C:\Program Files\nodejs\node.exe" "C:\path\to\app.js" "--run"
Attempting to start this service using powershell thus
Start-Service wibble
produces an error message containing: Cannot start service wibble on computer '.'
Attempting to start this service from the management interface's "Start the Service" link with the service selected in the Services manager yields a different error message: Windows could not start the wibble service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion.
The following works, when executed from a powershell in same folder as the app. Clients can connect to and use the app successfully:
node app.js --run
However, the powershell stops at that point until CTRL+C stops the service.
Here's the JS I added to the app to get it to run as a service:
var service = require('os-service');
var fs = require('fs');
process.chdir(__dirname);
if (process.argv[2] == '--run') {
var fn = process.env.LOCALAPPDATA + '/path/to/wibble.log';
var logStream = fs.createWriteStream (fn);
service.run (logStream, function () {
'use strict';
service.stop (0);
});
// rest of app here
}
module.exports = app
Hopefully I've overlooked something dumb. My gratitude if you can spot it!

How to restart / update node express vhost site

Im working on couple of sites, both have 2 versions, live one and dev one.
http://timeprize.com and http://test.timeprize.com
They both run on the same port and server, using express vhost.
My vhost app looks very simple, and is basically this:
var evh = require('express-vhost'),
express = require('express');
/*... some more variable declarations ...*/
if ( site_enabled('test.timeprize.com') ) {
/**/
evh.register('test.timeprize.com', function() {
var app = express();
app = require("../test_app/app.js").run_app(http);
return app;
}() );
};
if ( site_enabled('timeprize.com') ) {
evh.register('timeprize.com', function() {
var app = express();
app = require("../live_app/app.js").run_app(http);
return app;
}() );
};
/*... more sites below ...*/
Im running the code above using "forever" process.
And now the problem.
Since the test site, and live site are both running on the same server/port using same process, how do i update/restart the test site, without interrupting the live site ?
To address the:
Since the test site, and live site are both running on the same server/port using same process
This shouldn't occur. Your server should throw an error when a second program attempts to listen to a port already occupied.
To answer the real question:
How do I update/restart the test site, without interrupting the live site?
The answer is don't use forever. Yes, it's easy and whatever but for servers with multiple applications running in sync, the must use program is PM2
Install globally:
[sudo] npm i -g pm2
Start your apps, sudo if it listens to a port below 1024:
[sudo] pm2 start app.js
Restart your apps with:
[sudo] pm2 restart [all|name]
OR
[sudo] pm2 gracefulReload [all|name]
Check out documentation

nodeJS/ test my js file

I wrote the next try.js file
var net = require('net')
function create(r){
var server = net.createServer(function(socket) {
socket.write('hello\n');
socket.end('world\n');
});
server.listen(8000);
}
and I want to check if this function works well. So I want to excute it, and then go to localhost:8000 and check if I get the Hello massage. I try to go to dictionary of the try.js file via the node console, but I don't see any option for to do this.
the way you execute a nodejs app is by commandline
node try.js
then you can navigate to localhost:8000 but what you made there arnt a http server so browsers arnt going to get the response. telenet should give you the response 'hello world'. but more likely youre after the http insted of net server

Resources