-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
169 lines (143 loc) · 6.05 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
// set up ========================
var express = require('express');
var app = express(); // create our app w/ express
var mongoose = require('mongoose'); // mongoose for mongodb
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)
// configuration =================
mongoose.connect('mongodb://localhost/montagna', {useNewUrlParser: true, useUnifiedTopology: true}); // connect to mongoDB database locally
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(methodOverride());
// define model =================
// Todo -> Diary
var Schema = mongoose.Schema;
var Diary = mongoose.model('Diary', new Schema(
{
id : Number,
date : String,
courseType : String,
place : String,
partners : String,
description : String,
descriptionDetail : String,
descriptionUrl : String,
photoUrl : String
}
),
'diary'
);
// routes ======================================================================
// api ---------------------------------------------------------------------
// get all todos
// todos -> courses
app.get('/api/courses', function(req, res) {
console.log('get All: ');
// use mongoose to get all todos in the database
Diary.find(function(err, diary) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(diary); // return all todos in JSON format
});
});
app.get('/api/courses/:mongoId', function(req, res) {
// use mongoose to get all todos in the database
console.log('mongoID: '+ req.params.mongoId);
Diary.find({'_id' : req.params.mongoId },function(err, diary) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(diary); // return all todos in JSON format
});
});
// create todo and send back all todos after creation
app.post('/api/courses', function(req, res) {
// create a todo, information comes from AJAX request from Angular
var max=0;
//Get the max id
Diary.findOne({})
.sort({id: -1})
//.sort('-id')
.exec(function(err, doc)
{
if(err || isNaN(doc.id)){
console.log("error: " + err + " doc " + doc)
}
else{
max = doc.id;
Diary.create({
id : max+1,
date : req.body.date,
courseType : req.body.courseType,
place : req.body.place,
partners : req.body.partners,
description : req.body.description,
descriptionDetail : req.body.descriptionDetail,
descriptionUrl : req.body.descriptionUrl,
photoUrl : req.body.photoUrl,
done : false
}, function(err, course) {
if (err){
res.send(err);
console.log("error: " + err);
}
else{
Diary.find(function(err, courses) {
if (err)
res.send(err)
res.json(courses);
});
}
});
}
}
);
});
app.put('/api/courses/', function(req, res) {
// update course, information comes from AJAX request from Angular
Diary.findByIdAndUpdate(req.body._id,req.body, {
}, function(err, todo) {
if (err)
res.send(err);
console.log("In findByIdAndUpdate... " + req.body)
// get and return all the todos after you create another
Diary.find(function(err, courses) {
if (err)
res.send(err)
res.json(courses);
});
});
});
// delete a todo
app.delete('/api/courses/:mongoId', function(req, res) {
Diary.remove({
_id : req.params.mongoId
}, function(err, course) {
if (err){
console.log("Errore!!! " + err);
res.send(err);
}
else{
// get and return all the todos after you create another
Diary.find(function(err, courses) {
if (err)
res.send(err)
console.log("courses.length " + courses.length)
res.json(courses);
});
}
});
});
// application -------------------------------------------------------------
app.get('*', function(req, res) {
res.sendFile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
var today = new Date();
// listen (start app with node server.js) ======================================
app.listen(8089);
console.log(today.toLocaleString("en-US") + " - App listening on port 8089");