-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (50 loc) · 1.93 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
import 'dotenv/config';
import express from 'express';
import { createClient } from 'redis';
import wellknown from 'wellknown';
const { PORT, REDIS_URL } = process.env;
// Create a Redis client with details from the environment file.
const redisClient = createClient({
url: REDIS_URL
});
// Initialize Express.
const app = express();
app.set('views', new URL('./views', import.meta.url).pathname);
app.set('view engine', 'ejs');
app.use(express.static('static'));
app.use(express.json());
// Connect to Redis.
await redisClient.connect();
// Serve the home page.
app.get('/', async (req, res) => {
return res.render('homepage');
});
// Perform a polygon search and return the results.
// Expects the body to be a GeoJSON representation:
// https://en.wikipedia.org/wiki/GeoJSON
app.post('/search', async (req, res) => {
// Example query:
// FT.SEARCH idx:stations "@position:[within $poly]" RETURN 1 name PARAMS 2 poly "POLYGON((-122.387096 37.724491, -122.360487 38.802250, -122.521058 37.800800, -122.505826 37.705039, -122.387096 37.724491))" DIALECT 3
const wktString = wellknown.stringify(req.body.polygon);
const featuresClause = `${req.body.parking ? '@parking:{true}' : ''} ${req.body.lockers ? '@lockers:{true}' : ''} ${req.body.bikeRacks ? '@bikeRacks:{true}' : ''}`.trim();
const searchCommand = [
'FT.SEARCH', 'idx:stations', `@position:[within $poly] ${featuresClause}`, 'PARAMS', '2', 'poly', wktString, 'DIALECT', '3', 'LIMIT', '0', '100'
];
const searchResponse = await redisClient.sendCommand(searchCommand);
const matchingStations = [];
if (searchResponse[0] > 0) {
for (let n = 1; n < searchResponse.length; n += 2) {
matchingStations.push({
key: searchResponse[n],
...(JSON.parse(searchResponse[n+1][1])[0])
})
}
}
return res.json({
data: matchingStations
});
});
// Start the Express server.
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}.`);
});