Edie Johnny
Edie Johnny

Reputation: 523

How to set up Node.JS with Express and Socket.IO?

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

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

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.

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.

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

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

Upvotes: 1

Views: 830

Answers (2)

Edie Johnny
Edie Johnny

Reputation: 523

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

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

And then saving it with the command:

/sbin/service iptables save

CentOS6 based system.

Upvotes: 1

Santiago Rebella
Santiago Rebella

Reputation: 2449

Your actual example will work going to localhost:3800

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

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

Upvotes: 0

Related Questions