Skip to content

Part 2: Building the server with Node.js and Socket.io

Sergi Almar edited this page Nov 20, 2013 · 8 revisions

Creating the Node.js project

In order to have the skeleton of the project, we'll rely on express.js (the sinatra-like framework). Install express globally with the following command:

sudo npm install –g express

Now let's create an express based app called mobos-chat

express mobos-chat

The following structure will be created

   
create : mobos-chat
   create : mobos-chat/package.json
   create : mobos-chat/app.js
   create : mobos-chat/public
   create : mobos-chat/views
   create : mobos-chat/views/layout.jade
   create : mobos-chat/views/index.jade
   create : mobos-chat/public/images
   create : mobos-chat/routes
   create : mobos-chat/routes/index.js
   create : mobos-chat/routes/user.js
   create : mobos-chat/public/stylesheets
   create : mobos-chat/public/stylesheets/style.css
   create : mobos-chat/public/javascripts

Notice that a file called package.json containing the dependencies has been created. Execute npm install to add them to the project.

The app.js is the entry point to our application, it imports the required modules, defines some routings and creates the http server. Open and take a look at it. This should be enough to start our application.

Execute node app or npm start to start the app. Check that typing 'http://localhost:3000/' in your browser gives you the welcome page.

Adding Socket.io dependency

If you open the

Clone this wiki locally