Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
192 lines (174 loc) · 4.54 KB
/
Copy pathserver.js
File metadata and controls
192 lines (174 loc) · 4.54 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
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
import express from 'express';
import cors from 'cors';
import mongoose from 'mongoose';
import listEndpoints from 'express-list-endpoints';
const mongoUrl = process.env.MONGO_URL || 'mongodb://localhost/happyThoughts';
mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = Promise;
// Defines the port the app will run on. Defaults to 8080, but can be
// overridden when starting the server. For example:
//
// PORT=9000 npm start
const port = process.env.PORT || 8080;
const app = express();
//Schema for a more clean and dynamic approach
const ThoughtSchema = new mongoose.Schema({
message: {
type: String,
minlength: 5,
maxlength: 140,
trim: true,
required: true,
},
// categories: {
// type: String,
// enum: [
// 'Food thought',
// 'Project thought',
// 'Meditation thought',
// 'Travel thought',
// 'Other thought',
// ],
// required: true,
// },
hearts: {
type: Number,
default: 0,
},
createdAt: {
type: Number,
default: () => Date.now(),
},
author: {
type: String,
default: 'Anonymous',
},
});
const Thought = mongoose.model('Thought', ThoughtSchema);
// Add middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());
//----ENDPOINTS------
// Start defining your routes here
app.get('/', (req, res) => {
res.send(
'Hello world, Welcome to Madelene Happy Thoughts API - see this API live at 👉:https://mt-dotse-happy-thoughts.netlify.app/'
);
});
//Endpoint showing all the possible enpoints in the app
app.get('/endpoints', (req, res) => {
res.send(listEndpoints(app));
});
//Endpoint to get the most recent 20 thoughts
app.get('/thoughts', async (req, res) => {
try {
const allThoughts = await Thought.find()
.sort({ createdAt: 'desc' })
//.skip(5) to skip the first two objects
.limit(20);
res.status(200).json(allThoughts);
} catch (error) {
res.status(404).json({
message: 'Can not find thoughts',
errors: error.errors,
success: false,
});
}
});
//Endpoint to post messages, categories, author
// async/await form
app.post('/thoughts', async (req, res) => {
const { message, categories, author } = req.body;
try {
const newThought = await new Thought({
message,
author,
}).save();
//status (201) indicates success, more specific than (200)
res.status(201).json({ response: newThought, success: true });
} catch (error) {
res.status(400).json({
message: 'Can not save the thought',
error: error.error,
success: false,
});
}
});
// promise form
// app.post('/thoughts', (req, res) => {
// const { message} = req.body;
// new Thought({ message })
// .save()
// .then((data) => {
// res.status(201).json({ response: data, success: true });
// })
// .catch((error) => {
// res.status(400).json({ response: error, success: false });
// });
// });
// mongoose form
// app.post('/thought', (req, res) => {
// const { message } = req.body;
// new Thought({ message}).save((error, data) => {
// if (error) {
// res.status(400).json({ response: error, success: false });
// } else {
// res.status(201).json({ response: data, success: true });
// }
// });
// });
//Enpoint to increase the hearts/likes
app.post('/thoughts/:thoughtId/like', async (req, res) => {
const { thoughtId } = req.params;
try {
const updatedHeart = await Thought.findByIdAndUpdate(
thoughtId,
{
$inc: {
hearts: 1,
},
},
{
new: true,
}
);
if (updatedHeart) {
res.status(201).json({ response: updatedHeart, success: true });
} else {
res.status(404).json({ message: 'Not found!', success: false });
}
} catch (error) {
res.status(400).json({
message: 'Can not update the heart/like',
errors: error.error,
success: false,
});
}
});
//Endpoint to delete a thought
app.delete('/thoughts/:id/', async (req, res) => {
const { id } = req.params;
try {
const deletedThoughts = await Thought.findOneAndDelete({ _id: id }); //findOneAndDelete()
//status 204 means no content and should be used in combination with deleteOne
//status 200 should go with findOneAndDeleteOne
if (deletedThoughts) {
res.status(200).json({ response: deletedThoughts, success: true });
} else {
res
.status(404)
.json({ response: 'Can not find the thought', success: false });
}
} catch (error) {
res.status(400).json({
message: 'Can not delete the thought',
errors: error.error,
success: false,
});
}
});
// Start the server
app.listen(port, () => {
// eslint-disable-next-line
console.log(`Server running on http://localhost:${port}`);
});