-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (46 loc) · 1.54 KB
/
Copy pathserver.js
File metadata and controls
57 lines (46 loc) · 1.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
const express = require("express");
const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectID;
const bodyParser = require('body-parser');
const app = express();
let db;
app.use(bodyParser.json());
app.set("port", process.env.PORT || 3001);
// Express only serves static assets in production
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
app.get('/api/items/:category', function(req, res) {
let cat = req.params.category;
let q = {category: cat};
if (cat === "all") { q = {};}
db.collection("items").find(q).toArray(function(err, docs) {
res.json(docs);
});
});
app.get('/api/search/:term', function(req, res) {
let term = req.params.term;
//Get all items where it's category or name contains search term.
let q = {
$or: [
{category: {'$regex' : term, '$options' : 'i'}},
{name: {'$regex' : term, '$options' : 'i'}},
{brand: {'$regex' : term, '$options' : 'i'}}
]
};
if (term === "all") { q = {};}
db.collection("items").find(q).toArray(function(err, docs) {
res.json(docs);
});
});
app.get('/api/item/:id', function(req, res) {
db.collection("items").find({_id: ObjectId(req.params.id)}).limit(1).next(function(err, docs) {
res.json(docs);
});
});
MongoClient.connect('mongodb://localhost/storeact', function(err, dbConnection) {
db = dbConnection;
app.listen(app.get("port"), () => {
console.log(`Find the server at: http://localhost:${app.get("port")}/`); // eslint-disable-line no-console
});
});