node.js server external access not working - javascript

I have set up a node.js server that is supposed to serve HTML files from a directory to clients. It should also log any connecting and disconnecting clients. Both work perfectly, but only locally. I have already forwarded traffic on port 3000 to my server and deactivated its firewall. What else can I do to enable external access?
var express = require('express');
var socket = require('socket.io');
//app setup
var app = express();
var server = app.listen(3000, ready);
function ready(){
console.log('setup completed\nlistening on port 3000\n\n');
}
app.use(express.static('public'));
//socket setup
var io = socket(server);
io.on('connection', newConnection);
function newConnection(socket){
//some code
}

i have already forwarded traffic on port 3000 to my server
Thats the wrong way round. Your server listens on port 3000, but webservers offer there service under Port 80, so you have to forward Port 80 to Port 3000.
and deactivated its firewall.
Bad idea. Just open one port (80 or 3000 depending on where you do port forwarding). And usually Routers on the way will also block certain ingoing connections, so check that too.

Related

How to convert local server to online server Node.js

I've been developing this game for a school project. It is supposed to be online multiplayer, but this far i have online used it locally. I can figure out how to change my server code for it to be able to act as a "real" online server.
The server code:
// Dependencies
var express = require('express');
var http = require('http');
var path = require('path');
var socketIO = require('socket.io');
var app = express();
var server = http.Server(app);
app.set('port', 5000);
app.use('/static', express.static(__dirname + '/static'));
// Routing
app.get('/', function(request, response)
{
response.sendFile(path.join(__dirname, 'index.html'));
});
// Starts the server.
server.listen(5000, function()
{
console.log('Starting server on port 5000');
});
// Add the WebSocket handlers
io.on('connection', function(socket)
{
console.log('New player arrived');
});
Would appreciate any help greatly.
Most of the shared servers run at port 8080
so you may change the port then upload it into the hosting, If you just need it to be live u can use https://heroku.com/ so u can deploy it there
also here's quick option
npm install -g localtunnel
lt --port 8000
You will receive a URL, for example, https://school.localtunnel.me, that you can share with anyone for as long as your local instance of lt remains active. Any requests will be routed to your local service at the specified port
For more info: https://localtunnel.github.io/www/

why does express server listens to different port than my react app is on

My react app runs on default http://localhost:3000/ (which means that app runs on port 3000 as I've figured out), however when I want to get data from client to server, I have to listen to a different port. Here's the code:
const express = require("express");
const server = express();
const body_parser = require("body-parser");
const port = 4000;
server.listen(port, () => {
console.log(`Server listening at ${port}`);
When the server listens to port 4000 or any other port except 3000 everything works fine(I can receive and send data) but my code throws error whenever I start listening to 3000. I don't understand how this works, shouldn't I be listening to 3000 to connect to my react app? or am I missing some fundamental knowledge?
If you are running each app separately, then you have to run each app on a different port.
You will only need to run the server and listen to the same port if you are serving your front-end app from the server.

Node.js + Apache - https://localhost:3000/socket.io/ ERR_CONNECTION_REFUSED

I am implementing a simple game in Node.js. I have a client.js for my client side code, and a server.js running on a remote server, which both use sockets to communicate on port 3000
I am also running Apache on port 80, and using ProxyPass in my apache configuration file, to route the url mywebsite.io/agario to my nodejs server.
<Location /agario>
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
I am also using cloudflare to route my webserver 167.179.xx.xx through the url https://agario.mywebsite.io for SSL so that I can use HTTPS.
The problem
When I try to connect to my website https://agario.mywebsite.io/agario I am receiving the following error:
socket.io-1.4.5.js:1 GET https://localhost:3000/socket.io/?EIO=3&transport=polling&t=MakAMgZ net::ERR_CONNECTION_REFUSED
I am unclear why my client code is trying to connect to localhost, when I have specified in the code to connect to the remote server. Potentially I am just confused on how to run the node.js server as this is my first taste of Node.js and sockets.
client.js
...
var socket;
socket = io.connect('https://agario.mywebsite.io/agario');
...
server.js
var app = express();
var server = app.listen(3000, listen);
// This call back just tells us that the server has started
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
}
app.use(express.static('public'));
var io = require('socket.io')(server);
io.sockets.on('connection',
function(socket) {
console.log("We have a new client: " + socket.id);
...
});
If I have missed out any vital information please let me know and I will update my question, thank you.
You server is listening on port 3000 and you're trying to connect with it via 443, you should try something like this
socket.connect('https://ip:3000');
However, if you're sure that ur client is using the same port as the server or u have a port forwarding then try to use netcat just to make sure the the problem is with your script not the network config :
nc -zv -w1 ip port

Express generator with socket.io configuration

Below code is my current configuration, it worked but I'm confused.
server side
var server = require("http").Server(express);
var io = require("socket.io")(server);
server.listen(5000);
io.on('connection', function(client) {
});
cilent
var socket = io.connect('http://localhost:5000');
Why we need to create another server for socket at port 5000 for an application? can't socket use 3000? which is the express's running port. I removed the line of server.listen('5000') and do server.listen() and try connect to port 3000 at client side it doesn't work.
By default express 4 create a server and run it bin/www. I solved this issue by commenting out server.listen(port); in that file.

Nodejs on VPS only running on my network

Nodejs server which is installed on my VPS is accessible only on my network. People from outside world cannot access it. If its online, it should either be accessible all over the world or nowhere. What to do?
Code in my js file:
var app = require('express')();
var http = require('http').Server(app);
// Also tried http.listen(3000, "0.0.0.0", function(){
http.listen(3000, function(){
console.log('Server listening to port 3000');
});
Well, in your question you say that you want the Node app to be accessible to everyone in the world, or nobody at all. If you're VPS provider restricts you to only running things on an internal network, however, then it is impossible to do what you are asking.
The network rules will simply not allow it.
With that said, however, I'm going to make a recommendation for changing your Express application. Here's how it should look:
let express = require('express');
let app = express();
app.listen(3000);
The code above will bind your Node application to port 3000 in the simplest way possible using Express directly. This is probably what you want.
Also: please note that if you are intending to build a public service, you will need to likely do one of two things:
Bind your Express server to port 80 (for HTTP), or
Use a web server to proxy requests from port 80 (HTTP) to port 3000 (local).
My bad adding the site's IP as the second parameter of listen function solved it.
http.listen(3000, "xx.xxx.xx.xxx", function(){
console.log('Server listening to port 3000');
});

Categories

Resources