-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
183 lines (152 loc) · 4.55 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
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
var path = require('path');
var express = require('express');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var logger = require('morgan');
var bcrypt = require('bcryptjs');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var async = require('async');
var request = require('request');
var xml2js = require('xml2js');
var agenda = require('agenda')({ db: { address: 'localhost:27017/test' } });
var sugar = require('sugar');
var nodemailer = require('nodemailer');
var _ = require('lodash');
var recordSchema = new mongoose.Schema({
// _id: Number,
name: String,
time: Date,
temperature: Number,
humidity: Number,
sound: Number,
light: Number,
movement : Number,
// subscribers: [{
// type: mongoose.Schema.Types.ObjectId, ref: 'User'
// }]
});
var userSchema = new mongoose.Schema({
email: { type: String, unique: true },
password: String
});
userSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('password')) return next();
bcrypt.genSalt(10, function(err, salt) {
if (err) return next(err);
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
});
});
userSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};
var User = mongoose.model('User', userSchema);
var Record = mongoose.model('Record', recordSchema);
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, password, done) {
User.findOne({ email: email }, function(err, user) {
if (err) return done(err);
if (!user) return done(null, false);
user.comparePassword(password, function(err, isMatch) {
if (err) return done(err);
if (isMatch) return done(null, user);
return done(null, false);
});
});
}));
mongoose.connect('localhost');
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(methodOverride());
app.use(cookieParser());
app.use(session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
if (req.user) {
res.cookie('user', JSON.stringify(req.user));
}
next();
});
app.post('/api/login', passport.authenticate('local'), function(req, res) {
res.cookie('user', JSON.stringify(req.user));
res.send(req.user);
});
app.get('/api/logout', function(req, res) {
req.logout();
res.send(200);
});
app.post('/api/signup', function(req, res, next) {
var user = new User({
email: req.body.email,
password: req.body.password
});
user.save(function(err) {
if (err) return next(err);
res.send(200);
});
});
app.get('/api/records', function(req, res, next) {
var query = Record.find();
// query.limit(12);
query.exec(function(err, records) {
if (err) return next(err);
res.send(records);
});
});
app.get('/api/records/:id', function(req, res, next) {
Record.findById(req.params.id, function(err, record) {
if (err) return next(err);
res.send(record);
});
});
app.post('/api/records', function(req, res, next) {
var record = new Record({
name: req.body.name,
time: new Date(),
temperature: req.body.temperature,
humidity: req.body.humidity,
sound: req.body.sound,
light: req.body.light,
movement : req.body.movement
});
record.save(function(err) {
if (err) return next(err);
Record.find(function(err, records) {
if (err) return next(err);
res.send(records);
});
});
});
app.get('*', function(req, res) {
res.redirect('/#' + req.originalUrl);
});
app.use(function(err, req, res, next) {
console.error(err.stack);
res.send(500, { message: err.message });
});
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});