-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
77 lines (67 loc) · 1.66 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
const { initCrawler1 } = require('./service/crawler');
const { initWorker1 } = require('./service/worker');
const { Info } = require('./service/model');
require('dotenv').config();
const express = require('express');
const app = express();
const port = 3000;
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
(async() => {
await sleep(30000); // waiting for rabbitmq
initCrawler1();
await initWorker1();
})()
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(express.json());
app.get('/', (req, res) => {
// res.send('Hello World!')
res.render('index', {
SEARCH_API_URL: process.env.SEARCH_API_URL
});
});
app.post('/search', async (req, res, next) => {
const { longitude, latitude } = req.body;
console.log(longitude);
console.log(latitude);
const result = []
Info.aggregate(
[
{
'$geoNear': {
'near': {
'type': 'Point',
'coordinates': [ longitude, latitude ]
},
'spherical': true,
'distanceField': 'distance'
},
},
{ '$skip': 0 },
{ '$limit': 3 }
],
(err, nearestInfos) => {
if (err) throw err;
// console.log(nearestInfos);
for (const info of nearestInfos) {
console.log(info['station']);
console.log(info['available']);
console.log('==============================');
result.push({
station: info['station'],
available: info['available']
});
}
res.status(200).json({
result
});
}
);
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});