Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
Add very simple example server
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Feb 25, 2014
1 parent ebd1691 commit dc1add7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ However other platforms will fall back to a source compile: see [Source Build](#

# Usage

See the `example/server.js` and `test/osrm.test.js` for examples of using OSRM through this Node.js API.

# Setup

The `node-osrm` module consumes data processed by OSRM core.

This repository contains a Makefile that does this automatically:
Expand Down
36 changes: 36 additions & 0 deletions example/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var express = require('express');
var osrm = require('../');

var app = express();
var use_shared_memory = true;
var opts = new osrm.Options('test/data/berlin.ini', use_shared_memory);
var engine = new osrm.Engine(opts);

// Accepts a query like:
// http://localhost:8888?start=52.519930,13.438640&end=52.513191,13.415852
app.get('/', function(req, res) {
if (!req.query.start || !req.query.end) {
return res.json({"error":"invalid start and end query"});
}
var coordinates = [];
var start = req.query.start.split(',')
coordinates.push([+start[0],+start[1]]);
var end = req.query.end.split(',')
coordinates.push([+end[0],+end[1]]);
var query = new osrm.Query({
coordinates: coordinates,
alternateRoute: req.query.alternatives !== 'false'
});
engine.run(query, function(err, result) {
if (err) return res.json({"error":err.message});
try {
return res.json(JSON.parse(result));
} catch (err) {
return res.send(result);
}
});
});

console.log('Listening on port: ' + 8888);
app.listen(8888);

0 comments on commit dc1add7

Please sign in to comment.