forked from 92bondstreet/top-chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (53 loc) · 1.81 KB
/
server.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
const express = require('express');
const app = express();
const michelin = require('./js/michelin');
const lafourchette = require('./js/lafourchette');
var mongoose = require('mongoose');
var Restaurant = require('./models/restaurant');
mongoose.connect('mongodb://localhost/topchef');
const enable_michelin_scrapping = true // You can decide to re scrapp at each start or only refreshing deals
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/starred/:restaurantName', function (req, res) {
var name = req.params.restaurantName;
if(name == "all"){
Restaurant.find({}, function(err, restaurants) {
if (err) throw err;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(restaurants))
})
} else {
Restaurant.find({"name" : {$regex : ".*"+name+".*"}}, function(err, restaurants) {
if (err) throw err;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(restaurants))
})
}
})
app.get('/deals/:restaurantName', function (req, res) {
var name = req.params.restaurantName;
if(name == "all"){
Restaurant.find({hasDeals: true}, function(err, restaurants) {
if (err) throw err;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(restaurants))
})
} else {
Restaurant.find({hasDeals: true, "name" : {$regex : ".*"+name+".*"}}, function(err, restaurants) {
if (err) throw err;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(restaurants))
})
}
})
app.listen(6969, function () {
console.log('Express server is listening on port 6969!')
if(enable_michelin_scrapping) {
michelin.loadRestaurants()
} else {
lafourchette.checkDeals()
}
})