This is an introduction to server side programming in Node.js and was created for CoderDojo members. The aim of this game is to teach the fundamentals of programming and server side prorgamming in a fun way.
Install Node from http://nodejs.org
Create the project folder
mkdir coderdojo-start-node
Now create a folder called public, this is the folder whereyou will add the html, css and javascript code
cd coderdojo-start-node
mkdir public
npm is Node Package Manager allows you to install Node Packages, to run a web server we are going to use a node package called express will enable us to create a server easily that can accept web requests.
Now run
npm install express
Socket.io is a technology that allows browsers to easily communicate with the server over streams.
Now run
npm install socket.io
Now in the home directory candy-v-fruit save a new file called app.js. Inside app.js add the following code
var express = require('express');
var app = express();
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
This code creates your first server and first http call
From inside directory candy-v-fruit open a command terminal and run
node app
We are running app because the file is called app.js
You should see the following output from this command
$ node app
Listening on port 3000
This is tell you that a server has started on your machine at port 3000
You can stop the server at any time by running CTRL+C
Navigate to
http://localhost:3000
Now see what happens
Update app.js with the following code
var express = require('express');
var app = express();
var path = require('path');
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
var io = require('socket.io')(server);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/coderdojo', function(req, res){
res.send('Be cool');
});
io.on('connection', function (socket) {
console.log('emitting');
socket.on('chat', function (data) {
console.log('emitting 2');
console.log(data);
socket.broadcast.emit('chat', data);
});
});
Now open your browser and navigate to
http://localhost:3000