-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
172 lines (149 loc) · 3.36 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
// SERVER-SIDE JAVASCRIPT
//require express in our app
var express = require('express');
var morgan = require('morgan');
// generate a new express app and call it 'app'
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
// serve static files from public folder
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(morgan('combined'));
/************
* DATABASE *
************/
var db = require('./models');
/**********
* ROUTES *
**********/
/*
* HTML Endpoints
*/
app.get('/', function homepage(req, res) {
res.sendFile(__dirname + '/views/index.html');
});
app.get('/cities/:id', function homepage(req, res) {
console.log(req.params.id)
res.sendFile(__dirname + '/views/cities.html');
});
/*
* JSON API Endpoints
*/
app.get('/api/cities', function api_index(req, res) {
// send all cities as JSON response
db.City.find({}, function(err, city) {
if (err) {
return console.log("index error " + err);
}
res.json(city);
});
});
app.post('/api/cities', function citiesCreate(req, res) {
var newCity = new db.City({
location: {
lat: req.body.lat,
lng: req.body.lng
},
name: req.body.name,
logo: req.body.logo,
blurbs: req.body.blurbs
});
});
app.get('/api/cities/:id', function citiesShow(req, res) {
db.City.findOne({
_id: req.params.id
},
function(err, city) {
res.json(city);
}
);
});
app.delete('/api/cities/:id', function cityDestroy(req, res) {
cityId = req.params.id;
db.City.remove({
_id: cityId
},
function(err, removedCity) {
if (err) {
res.send("error")
};
res.json({
_id: cityId
});
}
);
});
app.get('/api/cities/:id/blurbs', function blurbsShow(req, res) {
db.City.findOne({
_id: req.params.id
},
function(err, city) {
res.json(city.blurbs);
}
);
});
app.post('/api/cities/:id/blurbs', function blurbsCreate(req, res) {
var cityId = req.params.id;
var newBlurb = new db.Blurb({
poster: req.body.poster,
textContent: req.body.textContent,
likes: 0,
imgPath: req.body.imgPath || ""
});
db.City.findOne({
_id: cityId
},
function(err, city) {
city.blurbs.unshift(newBlurb);
city.save();
}
);
res.json(newBlurb);
});
app.delete('/api/cities/:id/blurbs/:blurbId', function deleteBlurb(req, res) {
var blurbId = req.params.blurbId;
var cityId = req.params.id;
db.City.findOne({
_id: cityId
},
function(err, city) {
city.blurbs = city.blurbs.filter(function(blurb) {
return blurb._id != blurbId;
});
city.save();
}
);
res.send(blurbId);
});
app.put('/api/cities/:id/blurbs/:blurbId', function updateBlurb(req, res) {
var blurbId = req.params.blurbId;
var cityId = req.params.id;
db.City.findOne({
_id: cityId
},
function(err, city) {
var myBlurb = {};
city.blurbs.forEach(function(blurb) {
if (blurb._id == blurbId) {
blurb.likes += 1;
myBlurb = blurb;
}
});
city.save();
res.json({
_id: blurbId,
likes: myBlurb.likes
});
}
);
});
/**********
* SERVER *
**********/
// listen on port 3000
app.listen(process.env.PORT || 3000, function() {
console.log('Express server is running on http://localhost:3000/');
});