-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
154 lines (134 loc) · 4.26 KB
/
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//./mongo todos --> apre il db mongo "todos" (in mongodb/bin)
//./mongod --storageEngine=mmapv1 --dbpath /data/db --> start listener for mongodb connection (in mongodb/bin)
//node server --> start node server.js
var http = require('http');
var port = 1337;
var express = require('express'),
//pug = require('pug'),
todos = require('./app/routes/todos.js');
chats = require('./app/routes/chats.js');
var hbs = require('hbs');
var expressHbs = require('handlebars');
var app = express();
var mongo = require('mongodb');
var stanzeColl="stanze";
var chatColl="chat";
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('chat', server);
app.get('/todos', todos.findAll);
app.get('/todos/:id', todos.findById);
app.post('/todos', todos.addWine);
app.put('/todos/:id', todos.updateWine);
app.delete('/todos/:id', todos.deleteWine);
app.get('/chats', chats.findAll);
app.get('/chats/:id', chats.findById);
app.post('/chats', chats.addChat);
app.put('/chats/:id', chats.updateChat);
app.delete('/chats/:id', chats.deleteChat);
/*app.configure(function () {
app.use(express.logger('dev')); // 'default', 'short', 'tiny', 'dev'
app.use(express.bodyParser());
});*/
app.set('views',__dirname + '/app/views');
/*app.set('view engine','pug');*/
//app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'main.hbs'}));
app.set('view engine', 'hbs');
app.get('/', function(req, res){
global.stanzaID="";
db.collection(stanzeColl, function(err, collection)
{
collection.find().toArray(function(err, items) {
//console.log(items);
res.render(__dirname + '/app/views/index', { items: (items) });
});
});
});
app.get('/stanza/:id', function(req, res)
{
var id = new mongo.ObjectID(req.params.id)
global.stanzaID = id;
//console.log("IDSTANZA = " + global.stanzaID);
//console.log(global.stanzaID);
//console.log(JSON.stringify(id));
/* db.collection(stanzeColl, function(err, collection) {
collection.aggregate([
{
$lookup:
{
from: "chat",
localField: "id",
foreignField: "idStanza",
as: "messaggi"
}
}
], function(err, items) {
console.log(items);
res.render(__dirname + '/app/views/chat', { items: (items) });
});
});*/
db.collection(chatColl, function(err, collection)
{
collection.find({idStanza:global.stanzaID}).toArray(function(err, items)
{
//console.log("items where:" + JSON.stringify(items));
res.render(__dirname + '/app/views/chat', { items: (items) });
});
});
});
app.get('/chat', function(req, res){
db.collection('chat', function(err, collection) {
collection.find().toArray(function(err, items) {
console.log(items);
res.render(__dirname + '/app/views/chat', { items: (items) });
});
});
});
var io = require('socket.io').listen(app.listen(port));
openDB();
io.on('connection', function(socket)
{
//d console.log('a user connected');
socket.on('chat message', function(msg)
{
io.emit('chat message', msg);
console.log("IDSTANZA = " + global.stanzaID);
var msg = {msg:msg,idStanza:global.stanzaID};
db.collection(chatColl).insert(msg,{safe:true}, function(err,result){
//if (err) throw err;
if (err) {
console.log(err);
// res.send({'error':'An error has occurred'});
} else {
console.log('Success: ' + JSON.stringify(result));
// res.send(result[0]);
}
// db.close();
});
});
socket.on('stanze message', function(stanza){
var stanza = {nome:stanza};
db.collection(stanzeColl).insert(stanza,{safe:true}, function(err,result){
if (err)
{
console.log(err);
}
else
{
io.emit('stanze message', result.ops[0]);
console.log('Success: ' + JSON.stringify(result));
}
});
});
});
function openDB()
{
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'todos' database");
}
});
}
console.log('Listening on port '+ port +'...');