This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
135 lines (112 loc) · 2.94 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
/**
* Module dependencies.
*/
var express = require('express'),
app = module.exports = express.createServer(),
mongoose = require('mongoose'),
io = require('socket.io'),
socket = null,
db = null;
// Configuration
app.configure(function(){
app.use(express.logger());
app.use(express.errorHandler({dumpExceptions:true, showStack:true}));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
console.log('dev mode');
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
db = mongoose.connect('mongodb://localhost/nodechat');
});
app.configure('production', function(){
console.log('prod mode');
app.use(express.errorHandler());
db = mongoose.connect('mongodb://localhost/nodechat-prod');
});
app.configure('test', function() {
console.log('test mode');
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
db = mongoose.connect('mongodb://localhost/nodechat-test');
});
var model = require('./model.js');
// Routes
app.get('/', function(req, res){
res.render('index', {
locals: {
title: 'Welcome to node'
}
});
});
// list
app.get("/chats", function(req, res){
console.log("searching");
model.findAll(function(err, chats) {
if(err) {
throw err;
}
res.render("chats/index.jade", {
locals: {
chats:chats,
count: chats.length
}
});
});
});
// new action
app.get("/chats/new", function(req, res){
model.create("test chat", [], function(chat) {
res.redirect("/chats/" + chat.id);
});
});
// test
app.get("/test", function(req, res){
for (var i = 0, j = 0; i < 20; i++) {
model.create("test " + i,
[{
user: "user " + i,
timestamp: new Date(),
text: "This is some text"
},
{
user: "another user " + i,
timestamp: new Date(),
text: "This is some other text"
}], function(){
if (++j == 20) {
res.redirect("/chats");
}
});
}
});
// read
app.get("/chats/:id", function(req, res){
model.findById(req.params.id, function(err, chat) {
res.render('chats/chat.jade', {
locals: {
chat:chat
}
});
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(80);
socket = io.listen(app);
socket.on('connection', function(client){
// new client is here!
client.on('message', function(message){
model.addEntry(message.id, message.user, message.text, message.timestamp, function(entry) {
console.log("broadcasting " + JSON.stringify(entry));
client.broadcast(entry); // broadcast not working?
});
})
client.on('disconnect', function(){
})
});
console.log("Express server listening on port %d", app.address().port)
}