如何使用Express和Socket.IO设置Node.JS?

时间:2022-08-22 14:36:46

I'm trying to build a Node.JS server to listen on port 3800 of my CentOS server this way:

我正在尝试构建一个Node.JS服务器,以这种方式监听我的CentOS服务器的3800端口:

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
    res.send('<h1>Hello World!</h1>');
});

http.listen(3800, function(){
    console.log('Listening on port: 3800');
});

I have a domain and I configured this domain on my apache server with virtual host to listen on port 80. But when I try to access

我有一个域,我在我的apache服务器上配置了这个域,用虚拟主机监听端口80.但是当我尝试访问时

http://example.com:3800 or http://server_ip:3800

it's not working. The browsers keeps trying to connect and then I got the error.

它不起作用。浏览器不断尝试连接,然后我收到了错误。

I don't know what I did wrong, since I followed the tutorial. I searched other questions here, I tried to copy the code into my index.js and nothing. This simple "Hello World" is not showing and I can't access the server.

我不知道自己做错了什么,因为我遵循了教程。我在这里搜索了其他问题,我试图将代码复制到我的index.js中。这个简单的“Hello World”没有显示,我无法访问服务器。

I did the "node index.js" and on my server is showing "Listening on port: 3800" perfectly, I have root access and I did everything with the root user. I did the "npm install express" and "npm install socket.io" commands too, and I tried to make the package.json file and then "npm install". I searched another website and I tried their instructions with "npm install --save: express", won't work too.

我做了“node index.js”并在我的服务器上完美地显示“正在侦听端口:3800”,我有root访问权限,并且我使用root用户完成了所有操作。我也做了“npm install express”和“npm install socket.io”命令,我尝试制作package.json文件然后“npm install”。我搜索了另一个网站,我用“npm install --save:express”尝试了他们的说明,也不行。

I think it's a problem with my Linux configuration.

我认为这是我的Linux配置的一个问题。

My question is: how I can make this simple script work when I access http://example.com:3800?

我的问题是:当我访问http://example.com:3800时,如何使这个简单的脚本工作?

2 个解决方案

#1


1  

The correct answer to my question is to open the port at the iptables using this command:

我的问题的正确答案是使用以下命令在iptables上打开端口:

iptables -I INPUT -p tcp --dport 3800 -j ACCEPT

iptables -I INPUT -p tcp --dport 3800 -j ACCEPT

And then saving it with the command:

然后使用以下命令保存它:

/sbin/service iptables save

/ sbin / service iptables save

CentOS6 based system.

基于CentOS6的系统。

#2


0  

Your actual example will work going to localhost:3800

您的实际示例将工作转到localhost:3800

Im not sure how is that of running nodejs in an apache server :/

我不知道如何在apache服务器上运行nodejs:/

if it helps, I normally do it slightly different than your code:

如果它有帮助,我通常会比你的代码略有不同:

var app = require('express');
var http = require('http');

app.get('/', function(req, res){
    res.send('<h1>Hello World!</h1>');
});

app.listen(3800, function(){
    console.log('Listening on port: 3800');
});

EDIT-------------------------------

Your main issue here is that you are trying to use nodejs in Apache as I mention above, is not impossible but im sure is a pain to configure your server, and will not be the normal LAMP stack configuration, and you will probably run in some kind of bugs or cutted functionalitys, try to host your app in a node backend, something like openshift or heroku

你的主要问题是你正在尝试在Apache中使用nodejs,如上所述,并非不可能,但我确定配置你的服务器很痛苦,并且不会是正常的LAMP堆栈配置,你可能会运行一些一种错误或切割功能,尝试在节点后端托管您的应用程序,如openshift或heroku

#1


1  

The correct answer to my question is to open the port at the iptables using this command:

我的问题的正确答案是使用以下命令在iptables上打开端口:

iptables -I INPUT -p tcp --dport 3800 -j ACCEPT

iptables -I INPUT -p tcp --dport 3800 -j ACCEPT

And then saving it with the command:

然后使用以下命令保存它:

/sbin/service iptables save

/ sbin / service iptables save

CentOS6 based system.

基于CentOS6的系统。

#2


0  

Your actual example will work going to localhost:3800

您的实际示例将工作转到localhost:3800

Im not sure how is that of running nodejs in an apache server :/

我不知道如何在apache服务器上运行nodejs:/

if it helps, I normally do it slightly different than your code:

如果它有帮助,我通常会比你的代码略有不同:

var app = require('express');
var http = require('http');

app.get('/', function(req, res){
    res.send('<h1>Hello World!</h1>');
});

app.listen(3800, function(){
    console.log('Listening on port: 3800');
});

EDIT-------------------------------

Your main issue here is that you are trying to use nodejs in Apache as I mention above, is not impossible but im sure is a pain to configure your server, and will not be the normal LAMP stack configuration, and you will probably run in some kind of bugs or cutted functionalitys, try to host your app in a node backend, something like openshift or heroku

你的主要问题是你正在尝试在Apache中使用nodejs,如上所述,并非不可能,但我确定配置你的服务器很痛苦,并且不会是正常的LAMP堆栈配置,你可能会运行一些一种错误或切割功能,尝试在节点后端托管您的应用程序,如openshift或heroku