-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.js
127 lines (110 loc) · 3.79 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
var path = require("path");
var request = require('request');
const Person = require('./models/person')
// Set up the Express app
const app = express();
const MongodbMemoryServer = require('mongodb-memory-server');
const mongoServer = new MongodbMemoryServer.MongoMemoryServer({
binary: { version: "latest" },
instance: { port: 65210, dbName: "test" }
});
mongoServer.getConnectionString().then((uri) => {
// Connect to MongoDB - should be running locally
mongoose.connect(uri);
mongoose.Promise = global.Promise;
});
// Set up static files
app.use(express.static('public'));
app.use('/css', express.static(path.join(__dirname, 'public/styles')));
app.use('/scripts', express.static(path.join(__dirname, 'public/scripts')));
// Use body-parser to parse HTTP request parameters
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Error handling middleware
app.use(function (err, req, res, next) {
console.log(err); // To see properties of message in our console
res.status(422).send({ error: err.message });
});
var port = process.env.PORT || 3000;
// Starts the Express server, which will run locally @ localhost:3000
app.listen(port, function () {
console.log('App listening on port 3000!');
});
// Serves the index.html file (our basic frontend)
app.get('/', function (req, res) {
res.sendFile('index.html', { root: __dirname });
});
// GET route that displays all people (finds all Person objects)
app.get('/people', function (req, res, next) {
Person.find({}, function (err, result) {
if (err) {
console.log(err)
} else {
res.send(result); // Sends the result as JSON
}
});
});
// GET route that displays one person's friends
app.get('/people/:id', function (req, res, next) {
Person.findById(req.params.id, function (err, result) { // Finds person with id (param)
if (!err) {
res.send(result.friends); // Returns the person's friends array as JSON
} else {
throw err;
}
});
});
// POST route that adds a new Person object
app.post('/people', function (req, res, next) {
// First gets a random dog image URL
request('https://dog.ceo/api/breeds/image/random', function (error, response, body) {
if (!error && response.statusCode == 200) {
var person = new Person();
person.name = req.body.name; // Stores the 'name' string
person.dog = JSON.parse(body).message; // Stores the 'dog' image URL
person.friends = []; // Initializes an empty array of friends
person.save(function (err, person) { // Saves the Person object to the database
if (err) {
console.log(err);
} else {
res.send(person); // Returns the new object as JSON
}
})
}
});
});
// PUT route that adds a friend to a person
app.put('/people/:id', function (req, res, next) {
Person.findById(req.params.id, function (err, person) { // Finds a Person by id (param in URL)
person.friends.push(req.body.id); // Adds the friend with ID in POST parameters
person.save(function (err) { // Saves the Person object
if (err) {
console.log(err);
} else {
Person.findById(req.body.id, function (err, person) { // Same, but for the 2nd person
person.friends.push(req.params.id); // Saves the Person object
person.save(function (err) {
if (err) {
console.log(err);
} else {
res.send("Friendship between " + req.body.id + " and " + req.params.id + "created!");
}
})
});
}
})
});
});
// DELETE route that removes a Person object from the database
app.delete('/people/:id', function (req, res, next) {
Person.findByIdAndRemove(req.params.id, function (err, result) { // Finds by ID and remove
if (err) {
console.log(err);
} else {
res.send("Deleted person with id " + req.params.id);
}
});
});