Coding our game server
Now that our Node.js server is properly installed and our socket.io module is configured on both the server and the client, we can finally start coding. We will first go over the server code, cover all the games' interactions, and then move onto the client side and interact with the said server.
Creating a web server in less than 10 lines of code
One of the great things about Node.js is that it uses the familiar JavaScript language. It also provides us with a very powerful set of APIs to achieve our goals. We will start by creating our web server in order for our players to connect to it.
First, we will create a new file named app.js
in our server project. Inside this file, we will code our entire game server. First we will load the http
module using the require
function and we will store its reference in a variable named http
as follows:
var http = require('http');
Next, we will create our HTTP server using the following code and we will store its reference in the server...