-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
112 lines (101 loc) · 3.15 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
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
var Schema = mongoose.Schema;
// Connect to MongoDB
// mongoose.set('useFindAndModify', false);
mongoose.connect('mongodb://localhost:27017/animals', { useNewUrlParser: true });
// Parse requests of content-type 'application/json'
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.send('Hello World!');
});
// Mongoose schema
var camelSchema = new Schema({
color: { type: String },
position: { type: Number }
});
// Mongoose model
var Camel = mongoose.model('camels', camelSchema);
// Create a new camel
app.post('/camels', function(req, res, next) {
var camel = new Camel(req.body);
camel.save(function(err) {
if (err) { return next(err); }
res.status(201).json(camel);
});
});
// Return a list of all camels
app.get('/camels', function(req, res, next) {
Camel.find(function(err, camels) {
if (err) { return next(err); }
res.json({"data": camels});
});
});
// Return the camel with the given ID
app.get('/camels/:id', function(req, res, next) {
var id = req.params.id;
Camel.findById(req.params.id, function(err, camel) {
if (err) { return next(err); }
if (camel == null) {
return res.status(404).json({"message": "Camel not found"});
}
res.json(camel);
});
});
// Update the camel with the given ID
app.put('/camels/:id', function(req, res, next) {
var id = req.params.id;
Camel.findById(id, function(err, camel) {
if (err) { return next(err); }
if (camel == null) {
return res.status(404).json({"message": "Camel not found"});
}
camel.color = req.body.color;
camel.position = req.body.position;
camel.save();
res.json(camel);
});
});
// Partially update the camel with the given ID
app.patch('/camels/:id', function(req, res, next) {
var id = req.params.id;
var update = req.body;
// `new` determines whether to return the new (i.e., updated) version (default: false)
var options = {new: true};
Camel.findByIdAndUpdate(id, update, options, function(err, camel) {
if (err) { return next(err); }
if (camel == null) {
return res.status(404).json({"message": "Camel not found"});
}
res.json(camel);
});
});
// Delete the camel with the given ID
app.delete('/camels/:id', function(req, res, next) {
var id = req.params.id;
Camel.findOneAndDelete({_id: id}, function(err, camel) {
if (err) { return next(err); }
if (camel == null) {
return res.status(404).json({"message": "Camel not found"});
}
res.json(camel);
});
});
// Error handler (must be registered last)
app.use(function(err, req, res, next) {
console.error(err.stack);
var err_res = {
"message": err.message,
"error": {}
};
if (app.get('env') === 'development') {
err_res["error"] = err;
}
res.status(err.status || 500);
res.json(err_res);
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});