How to make Apache and Node.js with setup work?

Just starting with Node.js/ Ultimately I want to serve the pages from Apache while pushing and augmenting some of the content with Node.js server. This is just a little test to see how it all works. Here is my setup:

  • Apache running on port 8080
  • Node.js running on port 8000

I copied the index.html page (see below) to their respective root directories.

  1. When I open http://localhost:8000 everything works as expected and I can see the complete message exchange.

  2. When I open http://localhost:8080 everything appears to work (based on request and response headers, and Node.js log compared to 1.) but I can see no message exchange in either Node.js or Firebug log.

  3. When I first open the page served by Node.js, close it, then open the page served by Apache it works. I can see "CONNECTED", "DISCONNECTED", and then "CONNECTED" messages in the Node.js terminal.

The question is: how to make case 2. work?


<script src="http://localhost:8000/"></script>
    var socket = io.connect('http://localhost:8000');
    socket.on('server calling', function(data) {
        socket.emit('client response', {my : 'data'});
    socket.emit('client calling', {foo : 'bar'});


var app = require('http').createServer(handler);
var io = require('').listen(app);
var fs = require('fs');


function handler(req,res) {  

    path = req.url == "/" ? "./index.html" : "." + req.url; 
        function(err, data) {
            if(err) {
                return res.end('Error loading page.');

    io.sockets.on('connection', function (socket) {


        setInterval(function() {            
            socket.emit('server calling', { hello: 'world' }); 
        }, 2000);

        socket.on('client response', function(data) {
        socket.on('disconnect', function() {
        socket.on('client calling', function(data) {

I'm aware of the following questions on SO:

but I still cannot make it work...

I've tried:

  • "change the path to /"

In the browser: ReferenceError: io is not defined. When I go to http://localhost:8000/ I get Welcome to response.

In the server log: info - unhandled url

  • "serve the from your Apache static server"

How would that work? Apache does not know how to handle the / and node_modules automagic?

  • dark_ruby suggested setting up a proxy

I changed the script source in the index.html page in Apache root directory to:

<script src="http://localhost:8080/"></script>

In Apache config, I added:

LoadModule proxy_module modules/
LoadModule proxy_http_module modules/
ProxyPass / http://localhost:8000/

With such configuration opening http://localhost:8080 had the same result as in 2. above:

$ node node-server.js
   info  - started
   debug - served static content /
   debug - client authorized
   info  - handshake authorized U9xPRw_0rRpsv-K9qVkS
   debug - setting request GET /
   debug - set heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
   debug - client authorized for 
   debug - websocket writing 1:: 
   debug - emitting heartbeat for client U9xPRw_0rRpsv-K9qVkS
   debug - websocket writing 2:: 
   debug - set heartbeat timeout for client U9xPRw_0rRpsv-K9qVkS
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client U9xPRw_0rRpsv-K9qVkS
   debug - set heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
   info  - transport end (undefined)
   debug - set close timeout for client U9xPRw_0rRpsv-K9qVkS
   debug - cleared close timeout for client U9xPRw_0rRpsv-K9qVkS
   debug - cleared heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
   debug - discarding transport

i.e. no messages.


Your io.sockets is located inside handler function. I'm assuming it will be called when you make a request to node JS server. Till then, node JS server never executes your IO code.

Try moving it out of the handler and add it in your app.js.

And no need to change the path or the proxy. The code you posted originally is just fine. The problem is not with loading file. But node server detecting io events.

Need Your Help

jQuery:IE only, input:checked returns 0 even when items selected

jquery internet-explorer

I'm seeing an error I can't replicate from a small subset of users. Here's the code:

How set order by clause in HQL by run time?

hibernate hql

Is there any way to set order by clause in HQL by run time. i.e. select obj from Example1 order by &lt;Here my column name and asc or desc these two thing want to set in run time>.