Book Image

Learning Node.js for Mobile Application Development

Book Image

Learning Node.js for Mobile Application Development

Overview of this book

Table of Contents (21 chapters)
Learning Node.js for Mobile Application Development
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
14
Creating an E-Commerce Application Using the Ionic Framework
Index

Creating the server


The first thing that we need to do is create a WebSocket server to relay messages between our clients. Find a suitable project folder, open your terminal/command line, and run the following:

npm init

This will create the basic Node.js project structure. You can enter whatever values you see fit:

{
  "name": "ionic-chat-server",
  "version": "1.0.0",
  "description": "A websocket server for chatting.",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "csvan",
  "license": "MIT"
}

Now, let's install the dependencies that we will need. Run the following command:

npm install socket.io

This will install socket.io, which is all we will need in order to get our server running.

Next, create the server.js file in the current folder and add the following content to it:

var http = require('http');
var url = require('url');
var fs = require('fs');
var server = http.createServer(function (req, res) {
  var parsedUrl ...