forked from summer-yue/lifestats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
141 lines (105 loc) · 4.96 KB
/
server.js
File metadata and controls
141 lines (105 loc) · 4.96 KB
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
// server.js is the main backend file that starts this node application
//set up
var mongoose = require('mongoose');
var todoModel = require('./models/todo.js');
var userModel = require('./models/user.js');
var express = require('express');
var app = express();
var routes = require('./routes.js');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
//configuration
mongoose.connect('mongodb://localhost/lifestats');
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ 'extended': 'true' }));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(methodOverride());
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
//Initialize Mongo models
todoModel.initializeTodo();
userModel.initializeUser();
db.once('open', function() {
//Return unfinished Todo items in JSON format
app.get('/api/todos', routes.displayUnfinishedTodos);
//Return all Todo items in JSON format
app.get('/api/allTodos', routes.displayAllTodos);
//Return all Todo items for a certain user in JSON format
app.get('/api/allTodos/:user_email', routes.displayAllTodosForUser);
//Return all unfinished Todo items for a certain user in JSON format
app.get('/api/allUnfinishedTodos/:user_email', routes.displayAllUnfinishedTodosForUser);
//Return a single item in JSON format with id
app.get('/api/todo/:todo_id', routes.getTodoById);
//Add the current new Todo to todo database
//If no error, display the whole todo list
app.post('/api/addTodos', routes.addTodoItem);
//Add new user to the database
app.post('/api/addUser', routes.createUser);
//Updating a todo item
app.post('/api/updateTodos/:todo_id', routes.editTodoItem);
//Delete a todo with todo_id
//If no error, display the whole todo list
app.delete('/api/deleteTodos/:todo_id', routes.deleteTodoItem);
//Display all users that have ever used the app after it started
app.get('/api/users', routes.displayAllUsers);
app.post('/api/addPomodoroTime/:pomodoro_id/:new_time', routes.addPomodoroTime);
//Updating a user info settings
app.post('/api/updateUsers/:email', routes.editUser);
//Updating a user's happiness settings
//req.body.trackHappiness is the new happiness tracking field, true of false
//$http.post('/api/setHappinessSetting/Summer', {trackHappiness: false})
app.post('/api/setHappinessSetting/:username', routes.editUserHappinessSetting);
//Updating a user's stress settings
//req.body.trackStress is the new stress tracking field, true of false
app.post('/api/setStressSetting/:username', routes.editUserStressSetting);
//Updating a user's Pomodoro settings
//req.body.trackPomodoro is the new Pomodoro tracking field, true of false
app.post('/api/setPomodoroSetting/:username', routes.editUserPomodoroSetting);
//Updating a user's Todo settings
//req.body.trackTodo is the new Todo tracking field, true of false
app.post('/api/setTodoSetting/:username', routes.editUserTodoSetting);
//Updating a user's PopupFrequency settings
//req.body.popupFrequency is the new PopupFrequency field, a string, for example, "1h" means every hour
app.post('/api/setPopupFrequency/:email', routes.editUserPopupFrequency);
//Adding a stressPoint dictionary {time:, stressLevel:} to user's stress record
app.post('/api/addUserStressPoint/:email', routes.addUserStressPoint);
//Adding a happinessPoint dictionary {time:, happinessLevel:} to user's happiness record
app.post('/api/addUserHappinessPoint/:email', routes.addUserHappinessPoint);
//Return a single user item in JSON format with email
app.get('/api/user/:email', routes.getUserByEmail);
//Return the boolean value if certain email exists
app.get('/api/userExists/:email', routes.doesUserExist);
app.get('/home', function(req, res) {
res.sendfile('./public/homepage.html');
});
app.get('/pomodoro', function(req, res) {
res.sendfile('./public/pomodoro.html');
});
app.get('/stats', function(req, res) {
res.sendfile('./public/stats.html');
});
app.get('/settings', function(req, res) {
res.sendfile('./public/settings.html');
});
app.get('/todo', function(req, res) {
res.sendfile('./public/todo.html');
});
app.get('/happiness', function(req, res) {
res.sendfile('./public/stats-html/happiness.html');
});
app.get('/stress', function(req, res) {
res.sendfile('./public/stats-html/stress.html');
});
app.get('/productivity', function(req, res) {
res.sendfile('./public/stats-html/productivity.html');
});
app.get('/compactTodo', function(req, res) {
res.sendfile('./public/compact_todo.html');
});
});
//Listen (start app with node server.js)
app.listen(8080);
console.log("LifeStats running on port 8080");