-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
222 lines (194 loc) · 5.73 KB
/
app.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//require dependencies
var express = require('express');
var models = require('./models');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var dbUrl = process.env.MONGOHQ_URL || 'mongodb://localhost:27017/chat';
//global variables
staticFilePath = __dirname + '/public/';
//mongoose
var mongoose = require('mongoose');
mongoose.connect(dbUrl, {safe: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
});
//middleware
var logger = require('morgan'),
errorHandler = require('errorhandler'),
bodyParser = require('body-parser'),
jwt = require('jsonwebtoken'),
methodOverride = require('method-override');
uuident = require('node-uuid');
var app = express();
app.locals.appTitle = 'Chat';
//middleware that exposes Mongoose models in each Express.js route via a req object
app.use(function(req, res, next) {
if (!models.User || !models.Conversation || !models.Woo || !models.Message) return next(new Error('No models.'));
req.models = models;
return next();
});
//configure settings
app.set('port', process.env.PORT || 3003);
app.set('secret', process.env.SECRET || 'supersecret');
//use middleware
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(methodOverride());
app.use(require('stylus').middleware(__dirname + 'public'));
app.use(express.static(path.join(__dirname, 'public')));
//error handling if environment is development
if ('development' == app.get('env')) {
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
}
else if ('production' == app.get('env')) {
app.use(errorHandler());
}
//signup route
app.post('/api/users', function(req, res) {
//async. User.findOne won't fire unless data is sent back
process.nextTick(function() {
models.User.findOne( {
email: req.body.email
}, function(err, user) {
if (err) throw err;
if (user) {
res.json({ success: false, message: 'Signup failed! User with given email already exists.' });
}
else {
var newUser = new models.User();
newUser.firstName = req.body.firstName;
newUser.lastName = req.body.lastName;
newUser.email = req.body.email;
newUser.password = newUser.generateHash(req.body.password);
newUser.uuid = uuident.v4()
if (newUser.firstName && newUser.lastName && newUser.email && newUser.password && newUser.uuid) {
newUser.save(function(err){
if (err) {
throw err;
}
res.json({
success: true,
message: 'User successfully created!',
});
});
}
else {
res.json({
success: false,
message: 'Failed to generate new user. At least one field is missing',
});
}
}
});
});
});
//authenticate
app.post('/api/authenticate', function(req, res) {
models.User.findOne({
email: req.body.email
}, function(err, user) {
if (err) throw err;
if (!user) {
res.json({ success: false, message: 'Authentication failed. User not found' });
}
else {
if (user.validPassword(req.body.password)) {
var token = jwt.sign(user, app.get('secret'), {
expiresInMinutes: 1440 // expires in 24 hours
});
res.json({
success: true,
message: 'Token successfully generated!',
uuid: user.uuid,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
friends: user.friends,
token: token
});
}
else {
res.json({ success: false, message: 'Authentication failed. Wrong password.' });
}
}
});
});
//static images
app.get('/api/static/:filename', function(req, res) {
var filePath = staticFilePath + "img/" + req.params.filename;
res.sendFile(filePath, "base64");
//var img = fs.readFileSync(filePath, "base64");
});
//add authentication middleware middleware -- NEEDS TO BE AFTER api/authenticate
app.all('/api/*', function(req, res, next) {
var token = req.body.token || req.query.token || req.headers['x-auth-token'];
if (token) {
jwt.verify(token, app.get('secret'), function(err, decoded) {
if (err) {
return res.json({ success: false, message: 'Failed to authenticate token.' });
}
else {
req.decoded = decoded;
next();
}
});
}
else {
console.log('did not have token');
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
});
//User API routes
app.put('/api/users/:uuid', routes.userAPI.updateUser);
app.get('/api/users', routes.userAPI.getUsers);
app.get('/api/users/:uuid', routes.userAPI.getUser);
app.get('/api/users/:uuid/friends', routes.userAPI.getFriendsForUser);
app.get('/api/users/:uuid/conversations', routes.userAPI.getConversationsForUser);
//Conversation API routes
app.post('/api/conversations', routes.conversationAPI.createNewConversation);
app.get('/api/conversations/:uuid', routes.conversationAPI.getConversation);
app.put('/api/conversations/:uuid', routes.conversationAPI.updateConversation);
//Woo API routes
app.get('/api/woos', routes.wooAPI.getWoos);
app.get('/api/woos/:uuid', routes.wooAPI.getWoo);
//catch-all error 404 response
app.all('*', function(req, res) {
res.sendStatus(404);
});
//start server
var server = http.createServer(app);
var boot = function() {
server.listen(app.get('port'), function() {
console.info('Express server listening on port ' + app.get('port'));
});
server.on('error', function(err) {
console.error(err);
});
}
var shutdown = function() {
server.close();
}
if (require.main === module) {
boot();
}
else {
console.info('Running app as a module');
exports.boot = boot;
exports.shutdown = shutdown;
exports.port = app.get('port');
}
//uncaught error handling
process.on('uncaughtException', function(err) {
console.error('uncaughtException: ', err.message);
console.error(err.stack);
process.exit(1);
});