-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
61 lines (53 loc) · 1.52 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
const express = require('express');
const cors = require('cors');
const bodyParser = require("body-parser");
const { route, closest, distance, topology } = require('./src/pgRouting');
const { exp_config } = require('./src/config');
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.get('/api', (req, res) => {
res.send('Welcome to PG Routing API Written in Node JS Express!');
});
app.get('/api/route', async (req, res, next) => {
const { start, end } = req.query;
route(start, end)
.then((result) => {
res.status(200).json(result);
})
.catch((reason) => {
res.status(500).json(reason);
});
});
app.get('/api/distance', async (req, res, next) => {
const { start, end } = req.query;
distance(start, end)
.then((result) => {
res.status(200).json(result);
})
.catch((reason) => {
res.status(500).json(reason);
});
});
app.get('/api/closest', async (req, res, next) => {
const { lat, lng } = req.query;
const buffer = values.buffer || 1;
const limit = values.limit || 1;
closest(lat, lng, buffer, limit)
.then((result) => {
res.status(200).json(result);
})
.catch((reason) => {
res.status(500).json(reason);
});
});
app.get('/api/topology', async (req, res, next) => {
topology()
.then((result) => {
res.status(200).json({ result: "Okay" });
})
.catch((reason) => {
res.status(500).json(reason);
});
});
app.listen(exp_config.port, () => console.log(`app listening on port ${exp_config.serverPort}!`));