-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
100 lines (91 loc) · 2.81 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
const express = require('express');
// request body parser req.body.xxx
const bodyParser = require('body-parser');
const mongojs = require('mongojs');
const cors = require('cors');
// connecting with DB, first param is the name of the db, 2nd param is // the array of the collections we will be using
const db = mongojs('catalog', ['products']);
const app = express();
const PORT = 3000;
const corsOptions = {
origin: 'localhost',
optionsSuccessStatus: 200
}
// use middleware
app.use(bodyParser.json());
app.listen(PORT, () => {
console.log(`Server is listening to port ${PORT}`);
});
app.get('/', cors(corsOptions), (req, res, next) => {
// send back to browser
res.send('Please use /api/v1/products/');
});
// use v1 for different version of API
app.get('/api/v1/products/', cors(corsOptions), (req, res, next) => {
db.products.find((err, docs) => {
if (err) {
res.send(err);
return;
}
console.log('Product Found...');
res.json(docs);
});
});
app.get('/api/v1/products/:id', cors(corsOptions), (req, res, next) => {
db.products.findOne({_id: mongojs.ObjectID(req.params.id)}, (err, doc) => {
if (err) {
res.send(err);
return;
}
console.log(`Product ${req.params.id} found...`);
res.json(doc);
});
});
// add product
app.post('/api/v1/products/', cors(corsOptions), (req, res, next) => {
db.products.insert(req.body, (err, doc) => {
if (err) {
res.send(err);
return;
}
console.log('Adding a product');
res.json(doc);
})
});
// Update a product
app.put('/api/v1/products/:id', cors(corsOptions), (req, res, next) => {
// findAndModify https://docs.mongodb.com/manual/reference/command/findAndModify/
// findAndModify({query: {_id: xxx}, update: {$set: {}}, new: true}, (err, doc) => {})
// update certain fields using $set
// new: true meaning if it's not found in db, we add it as a new product
db.products.findAndModify({
query: {_id: mongojs.ObjectId(req. params.id)},
update:{
$set:{
name: req.body.name,
category: req.body.category,
price: req.body.price
}
},
new: true
}, (err, doc) => {
if (err) {
res.send(err);
return;
}
console.log('Updating a product...');
res.json(doc);
}
);
});
// delete a product
app.delete('/api/v1/products/:id', cors(corsOptions), (req, res, next) => {
db.products.remove({_id: mongojs.ObjectID(req.params.id)}, (err, doc) => {
if (err) {
res.send(err);
return;
}
console.log('Removing a product...');
res.json(doc);
})
});