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.
I'm experimenting with node.js and express.js.
When I try to connect to my web server from any computer in my network, it works, but then when I try to connect from outside network the connection times out.
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res) {
res.send("Hello World");
});
http.listen(3000, '0.0.0.0', function() {
console.log("Listening on port 3000!");
});
I just tested your code and I'm able to access the server from outside my local network by navigating to:
http://173.0.[my].[ip]:3000
So the code is correct. It could be that you need to open the port 3000 to the outside world. Here's how it can be accomplished.
Through your router admin interface
Here's mine for example:
Where 192.168.1.130 is the local IP of the PC I'm running the http server on.
Don't forget to click the Save settings button in that interface to apply the changes.
Using a tool like ngrok (mentioned by eddiezane)
Install ngrok through their website or without leaving the command prompt, with the ngrok node wrapper.
npm install ngrok -g
Start your http server and then run:
ngrok http 3000
Navigate to one of the url in front of Forwarding:
The free version is more for a quick test and less as a definitive way to expose a service in a production environnement since every time you restart ngrok, a new user-hostile url is given to you.
Other possible problems
It could also be that you need to add an exception to the firewall (if on windows).
To add to Emile's answer, I would check out ngrok which is an awesome tool that generates you a publicly accessible URL for a port on your local machine.
Here's a good blog post on it my buddy wrote.
I have a Node.js/Express.js app running on my server that only works on port 3000 and I'm trying to figure out why. Here's what I've found:
Without specifying a port (app.listen()), the app runs but the web page does not load.
On port 3001 (app.listen(3001)) or any other port that is not in use, the app runs but the web page does not load.
On port 2999, the app throws an error because something else is using that port.
On port 3000, the app runs and the web page loads fine.
I know that Express apps default to port 3000. But strangely, my app only runs when I explicitly make it run on port 3000 (app.listen(3000)).
I found this on line 220 of /usr/bin/express:
app.set(\'port\', process.env.PORT || 3000);
Which is doing as previously stated: setting the port to what is specified or to 3000 if nothing is specified.
How could I make my app work on a different port such as 8080 or 3001?
Thanks!
Edit: Code Sample (Very Simple Node/Express App)
var express = require("express");
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
// Only works on 3000 regardless of what I set environment port to or how I set [value] in app.set('port', [value]).
app.listen(3000);
The following works if you have something like this in your app.js:
http.createServer(app).listen(app.get('port'),
function(){
console.log("Express server listening on port " + app.get('port'));
});
Either explicitly hardcode your code to use the port you want, like:
app.set('port', process.env.PORT || 3000);
This code means set your port to the environment variable PORT or if that is undefined then set it to the literal 3000.
Or, use your environment to set the port. Setting it via the environment is used to help delineate between PRODUCTION and DEVELOPMENT and also a lot of Platforms as a Service use the environment to set the port according to their specs as well as internal Express configs. The following sets an environment key=value pair and then launches your app.
$ PORT=8080 node app.js
In reference to your code example, you want something like this:
var express = require("express");
var app = express();
// sets port 8080 to default or unless otherwise specified in the environment
app.set('port', process.env.PORT || 8080);
app.get('/', function(req, res){
res.send('hello world');
});
// Only works on 3000 regardless of what I set environment port to or how I set
// [value] in app.set('port', [value]).
// app.listen(3000);
app.listen(app.get('port'));
In bin/www, there is a line:
var port = normalizePort(process.env.PORT || '3000');
Try to modify it.
Try this
$ PORT=8080 node app.js
Try to locate the bin>www location and try to change the port number...
The default way to change the listening port on The Express framework is to modify the file named www in the bin folder.
There, you will find a line such as the following
var port = normalizePort(process.env.PORT || '3000');
Change the value 3000 to any port you wish.
This is valid for Express version 4.13.1
Just a note for Mac OS X and Linux users:
If you want to run your Node / Express app on a port number lower than 1024, you have to run as the superuser:
sudo PORT=80 node app.js
In the lastest version of code with express-generator (4.13.1) app.js is an exported module and the server is started in /bin/www using app.set('port', process.env.PORT || 3001) in app.js will be overridden by a similar statement in bin/www.
I just changed the statement in bin/www.
Noticed this was never resolved... You likely have a firewall in front of your machine blocking those ports, or iptables is set up to prevent the use of those ports.
Try running nmap -F localhost when you run your app (install nmap if you don't have it). If it appears that you're running the app on the correct port and you can't access it via a remote browser then there is some middleware or a physical firewall that's blocking the port.
Hope this helps!
The line you found just looks for the environmental variable PORT, if it's defined it uses it, otherwise uses the default port 3000. You have to define this environmental variable first (no need to be root)
export PORT=8080
node <your-app.js>
If you want to show something you're connected on 3000
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
I hope that will be helpful to you
Answer according to current version of express
If you talk about the current version of express, if you run app.listen() to start listening without specifying port, Express will chose a random port for your application, to find out about which port it is currently running on use
app.listen(0, () => {
console.log(app.address().port)
}
should output the port of your app. Moreover that first parameter 0 can be totally ignored but is not recommended
In app.js, just add...
process.env.PORT=2999;
This will isolate the PORT variable to the express application.
I am using the minimist package and the node startup arguments to control the port.
node server.js --port 4000
or
node server.js -p 4000
Inside server.js, the port can be determined by
var argv = parseArgs(process.argv.slice(2))
const port = argv.port || argv.p || 3000;
console.log(`Listening on port ${port}...`)
//....listen(port);
and it defaults to 3000 if no port is passed as an argument.
You can then use listen on the port variable.
Make sure you are running from that folder of your application, where you have the package.json.
I think the best way is to use dotenv package and set the port on the .env config file without to modify the file www inside the folder bin.
Just install the package with the command:
npm install dotenv
require it on your application:
require('dotenv').config()
Create a .env file in the root directory of your project, and add the port in it (for example) to listen on port 5000
PORT=5000
and that's it.
More info here
If you are using Nodemon my guess is the PORT 3000 is set in the nodemonConfig.
Check if that is the case.
Is there a good way of using sessions with Socket.io in Express 3.0? A way of getting the clients' session id in a safe way? So that I can send notices to members specific to their account and make private chats from member to member?
I'm using MySQL store in Express 3.0
I wrote a tiny module to abstract it, here's how its usage looks like. It was written and tested using express 3, socket.io 0.9.10 and the (default) MemoryStore from connect 2.4.5. It should work fine with other compatible stores.
var SessionSockets = require('session.socket.io')
, sessionSockets = new SessionSockets(io, sessionStore, cookieParser);
sessionSockets.on('connection', function (err, socket, session) {
//your regular socket.io code goes here
});
For more details on how it works see https://github.com/wcamarao/session.socket.io
You might want to pay attention to the part of the README where it says how to use it with your own session store key (I'm assuming your mysql store uses a name other than the default 'connect.sid').
You should check out express.io, a very simple micro-framework for express and socket.io integration. It handles express and socket.io sessions automatically.
npm install express.io
Check out the session support example here:
https://github.com/techpines/express.io/tree/master/examples#sessions
I did something slightly different to get it working. I read through a lot of posts on nodester github and came with the following solution....
Replace:
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
with:
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = require('socket.io').listen(server);
My plan is to continue with this workaround until issues around express3 and socket.io are resolved.
I wasn't sure if this is appropriate on the Ubuntu exchange or here but it is mostly code related i believe.
So I have created a neat working little web-chat application using socket.io which I've been developing on my Win10 pc using Git-bash and running it on my localhost with node.js and everything has been working just fine.
Thus I have come to a point where I would like to try out my app on my web-server.
Lets get more detailed what my Problems are:
I am unsure of how my server.js file should listen or open up the ports so to speak.
I have tried looking in the sockets.io chat example and tried their method yet the '../..' module leaves me confused.
Following the express.js site tutorial I actually get my server to respond listening on port 3000 yet my website returns the no socket.io/socket.io.js found.
this and this just lead to another localhost tutorial
In Short I have come to the point where when i do node server.js It seems to start listening here's the code to that part:
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var port = process.env.PORT || 3000;
app.get ('/', function(req,res) {
res.sendFile(__dirname + '/index.html');
});
app.use("/static", express.static('./static/'));
server.listen(3000);
But now the problem is is that my html file cannot seem to find the socket.io/socket.io.js file, even though i have installed sockets.io, i consulted to this stack question which should fix it, but it didnt thus leads me to believe theres a thicker more server side issue?
Versions i am using:
express: **4.16.4**;
node : **10.15.0**;
npm : **6.4.1**;
socket : **2.2.0**;
EDIT: Added my html code snips
html
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script src="static/index.js"></script>