node.js: How to run express and socket.io on the same port

The express web server and socket.io are very handy modules for node.js. Either of the modules can listen to the same port, because the modules use different protocols. But how can this be configured? The ‘How to use” section of the socket.io webpage has the solution, even though it is hard to find. The following code shows how to create a simple web server and a websocket communication on the same port.

var express = require("express");
var io = require('socket.io')
var port = 80;
var htmlDir = "./client/"

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


app.configure(function () {
    app.use(
        "/", 
        express.static(htmlDir)
    );
});

server.listen(80);
Advertisements
This entry was posted in Development, Node.js and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s