forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
35 lines (30 loc) · 1.08 KB
/
example.js
File metadata and controls
35 lines (30 loc) · 1.08 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
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import express from 'express';
import OSRM from '../lib/index.js';
const app = express();
const __dirname = dirname(fileURLToPath(import.meta.url));
const osrm = new OSRM(join(__dirname,"../test/data/ch/monaco.osrm"));
// Accepts a query like:
// http://localhost:8888?start=7.419758,43.731142&end=7.419505,43.736825
app.get('/', function(req, res) {
if (!req.query.start || !req.query.end) {
return res.json({"error":"invalid start and end query"});
}
const coordinates = [];
const start = req.query.start.split(',');
coordinates.push([+start[0],+start[1]]);
const end = req.query.end.split(',');
coordinates.push([+end[0],+end[1]]);
const query = {
coordinates: coordinates,
alternatives: req.query.alternatives !== 'false'
};
osrm.route(query, function(err, result) {
if (err) return res.json({"error":err.message});
return res.json(result);
});
});
const port = 8888;
console.log('Listening on port: ' + port);
app.listen(port);