-
Notifications
You must be signed in to change notification settings - Fork 11
/
nodejs-example-server.js
executable file
·86 lines (63 loc) · 1.67 KB
/
nodejs-example-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
'use strict';
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server),
port = 3333,
host = '0.0.0.0';
require('mootools');
io.set('log level', 1);
app.use(express.static('./example'));
app.get('/js/Epitome-min.js', function(req, res){
res.send(require('fs').readFileSync('./Epitome-min.js', 'utf-8'));
});
app.get('/Epitome-min.js', function(req, res){
res.send(require('fs').readFileSync('./Epitome-min.js', 'utf-8'));
});
server.listen(port);
(function(){
// output all the ips we listen to so others can connect
var os = require('os'),
ifaces = os.networkInterfaces(),
dev;
console.log('Express running on http://'+ host + ':' + port);
for (dev in ifaces){
ifaces[dev].forEach(function(details){
if (details.family == 'IPv4'){
host = details.address;
console.log('Express also available on http://'+ host + ':' + port);
}
});
}
}());
// use some epitome models and collections
var Model = require('./src/epitome-model'),
Collection = require('./src/epitome-collection');
var User = new Class({
Extends: Model,
options: {
defaults: {
title: 'Mr.'
}
}
});
var Users = new Class({
Extends: Collection,
model: User
});
var users = new Users([{
name: 'Bob',
surname: 'Roberts'
}]);
io.on('connection', function(socket){
socket.on('data:add', function(model){
users.addModel(model);
console.log('added new model', model);
// using Slick on the server...
var user = users.findOne('[surname='+model.surname+']');
console.log('Can use slick, found: ' + user.toJSON());
});
socket.emit('data', users.toJSON());
});