On Github dkundel / modern-web-apps
Created by Dominik Kundel / @dkundel
Time to apply our knowledge by building a small demo application :)
$> npm install socket.io
$> npm install grunt-cli -g
// file server.js var net = require('net'); var server = net.createServer(function (socket) { socket.pipe(socket); }); server.listen(1337, '127.0.0.1');
Run script:
$> node server.js
// file connection module.exports.handle = function (socket) { socket.pipe(socket); };
// file server.js var net = require('net'); var Connection = require('./connection') var server = net.createServer(Connection.handle); server.listen(1337, '127.0.0.1');
{ "name": "openjub-api", "version": "0.0.1", "description": "Building great tools for Jacobs University Bremen", "main": "server.js", "repository": { "type": "git", "url": "git://github.com/OpenJUB/openjub-api.git" }, "keywords": [ "Jacobs", "University", "OpenJUB" ], "author": "Dominik Kundel ", "license": "MIT", "bugs": { "url": "https://github.com/OpenJUB/openjub-api/issues" }, "homepage": "https://github.com/OpenJUB/openjub-api", "apidocFilename": "API.md", "dependencies": { "mongoose": "~3.8.7", "express": "~3.4.8", "mongodb": "~1.3.23", // ... "prompt": "~0.2.12", "request": "^2.34.0" }, "devDependencies": { "grunt": "~0.4.2", "grunt-contrib-clean": "~0.5.0", // ... "grunt-apidoc": "~0.3.0" } }
Install depencies:
$> npm install
$> npm install socket.io
// file server.js var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs') app.listen(80); function handler (req, res) { fs.readFile(__dirname + '/index.html', function (err, data) { if (err) { res.writeHead(500); return res.end('Error loading index.html'); } res.writeHead(200); res.end(data); }); } io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });
Run:
$> node server.js
// put this as a script block into your index.html // include before "/socket.io/socket.io.js" var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); });
$> npm install -g express
$> express --session awesomeApp $> cd awesomeApp $> npm install
$> npm install mongodb mongoose socket.io --save
$> node app.js
Coming soon...