简体   繁体   中英

node.js real time chat server

hey guys i made chat for my chat application on my local host with some guids i found on the internet and im struggling to add is to my real server on line, now it listening to port localhost:3000 but i dont know what sould i do for a real server, help me please!

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);

server.listen(process.env.PORT || 3000);
app.get('/',function(req,res){
   res.sendFile(__dirname + '/index.html');
});

Now you're gonna need to set up your server. I recommend digitalocean for a cheap cloud VPS. You can follow this tutorial.

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Basically you'll need to set up a process manager. For node i recommend PS2.

If you're looking to host your chat server from your residence, you will need to route external node traffic to the machine (or VM) which runs your node server.

You can do this by port forwarding . That means you'll need to go into your router's UI/settings panel and create a new port forwarding rule. Namely, you will probably want external traffic which arrives on port 80 to route to the local IP of your node server's machine on port 3000 .

Once this is done and the changes have been saved to your router config, you should already be able to put your public IP in a browser's URL bar and be directed to your web-facing node app.

Beyond the scope of this question is getting yourself a domain name, and attaching it to your public IP, either statically or dynamically depending on your needs. That way, you wouldn't have to enter your public IP in the URL bar, you would just have to enter your domain, ie myfirstchatapp.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM