Skip to content

Commit 5191648

Browse files
committed
feat: add mongodb-memory-server
1 parent 7925bf7 commit 5191648

4 files changed

Lines changed: 726 additions & 134 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

‎app.js‎

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ const Person = require('./models/person')
88
// Set up the Express app
99
const app = express();
1010

11-
// Connect to MongoDB - should be running locally
12-
mongoose.connect('mongodb://localhost/people');
13-
mongoose.Promise = global.Promise;
11+
const MongodbMemoryServer = require('mongodb-memory-server');
12+
13+
const mongoServer = new MongodbMemoryServer.MongoMemoryServer({
14+
binary: { version: "latest" },
15+
instance: { port: 65210, dbName: "test" }
16+
});
17+
18+
mongoServer.getConnectionString().then((uri) => {
19+
// Connect to MongoDB - should be running locally
20+
mongoose.connect(uri);
21+
mongoose.Promise = global.Promise;
22+
});
23+
24+
1425

1526
// Set up static files
1627
app.use(express.static('public'));
@@ -22,26 +33,26 @@ app.use(bodyParser.json());
2233
app.use(bodyParser.urlencoded({ extended: false }));
2334

2435
// Error handling middleware
25-
app.use(function(err, req, res, next){
26-
console.log(err); // To see properties of message in our console
27-
res.status(422).send({error: err.message});
36+
app.use(function (err, req, res, next) {
37+
console.log(err); // To see properties of message in our console
38+
res.status(422).send({ error: err.message });
2839
});
2940

3041
var port = process.env.PORT || 3000;
3142

3243
// Starts the Express server, which will run locally @ localhost:3000
33-
app.listen(port, function() {
34-
console.log('App listening on port 3000!');
44+
app.listen(port, function () {
45+
console.log('App listening on port 3000!');
3546
});
3647

3748
// Serves the index.html file (our basic frontend)
38-
app.get('/',function(req, res) {
39-
res.sendFile('index.html', {root: __dirname});
40-
});
49+
app.get('/', function (req, res) {
50+
res.sendFile('index.html', { root: __dirname });
51+
});
4152

4253
// GET route that displays all people (finds all Person objects)
43-
app.get('/people', function(req, res, next) {
44-
Person.find({}, function(err, result) {
54+
app.get('/people', function (req, res, next) {
55+
Person.find({}, function (err, result) {
4556
if (err) {
4657
console.log(err)
4758
} else {
@@ -51,8 +62,8 @@ app.get('/people', function(req, res, next) {
5162
});
5263

5364
// GET route that displays one person's friends
54-
app.get('/people/:id', function(req, res, next) {
55-
Person.findById(req.params.id, function(err, result) { // Finds person with id (param)
65+
app.get('/people/:id', function (req, res, next) {
66+
Person.findById(req.params.id, function (err, result) { // Finds person with id (param)
5667
if (!err) {
5768
res.send(result.friends); // Returns the person's friends array as JSON
5869
} else {
@@ -62,36 +73,36 @@ app.get('/people/:id', function(req, res, next) {
6273
});
6374

6475
// POST route that adds a new Person object
65-
app.post('/people', function(req, res, next) {
76+
app.post('/people', function (req, res, next) {
6677
// First gets a random dog image URL
6778
request('https://dog.ceo/api/breeds/image/random', function (error, response, body) {
6879
if (!error && response.statusCode == 200) {
6980
var person = new Person();
7081
person.name = req.body.name; // Stores the 'name' string
7182
person.dog = JSON.parse(body).message; // Stores the 'dog' image URL
7283
person.friends = []; // Initializes an empty array of friends
73-
person.save(function(err, person) { // Saves the Person object to the database
84+
person.save(function (err, person) { // Saves the Person object to the database
7485
if (err) {
7586
console.log(err);
7687
} else {
7788
res.send(person); // Returns the new object as JSON
7889
}
7990
})
80-
}
91+
}
8192
});
8293
});
8394

8495
// PUT route that adds a friend to a person
85-
app.put('/people/:id', function(req, res, next) {
86-
Person.findById(req.params.id, function(err, person) { // Finds a Person by id (param in URL)
96+
app.put('/people/:id', function (req, res, next) {
97+
Person.findById(req.params.id, function (err, person) { // Finds a Person by id (param in URL)
8798
person.friends.push(req.body.id); // Adds the friend with ID in POST parameters
88-
person.save(function(err) { // Saves the Person object
99+
person.save(function (err) { // Saves the Person object
89100
if (err) {
90101
console.log(err);
91102
} else {
92-
Person.findById(req.body.id, function(err, person) { // Same, but for the 2nd person
103+
Person.findById(req.body.id, function (err, person) { // Same, but for the 2nd person
93104
person.friends.push(req.params.id); // Saves the Person object
94-
person.save(function(err) {
105+
person.save(function (err) {
95106
if (err) {
96107
console.log(err);
97108
} else {
@@ -105,8 +116,8 @@ app.put('/people/:id', function(req, res, next) {
105116
});
106117

107118
// DELETE route that removes a Person object from the database
108-
app.delete('/people/:id', function(req, res, next) {
109-
Person.findByIdAndRemove(req.params.id, function(err, result) { // Finds by ID and remove
119+
app.delete('/people/:id', function (req, res, next) {
120+
Person.findByIdAndRemove(req.params.id, function (err, result) { // Finds by ID and remove
110121
if (err) {
111122
console.log(err);
112123
} else {

0 commit comments

Comments
 (0)