How to run a custom routing server with Node.js using OSM data

Mrs. Hansen taking the noon site on board the barkentine AURORA ( https://www.nps.gov/safr/learn/historyculture/maritimewomenhistory.htm)
A very happy navigating couple

OSM Data Download

Transfer the downloaded data to PostgreSQL using osm2pgrouting

CREATE EXTENSION postgis;
CREATE EXTENSION pgRouting;

osm2pgrouting -d routing_db -U postgres -h localhost -p 5432 -W your_password -f map1.osm -c “your\path\to\mapconfig.xml” — clean

Testing the Routing Data Using SQL

SELECT ST_AsGeoJSON(ST_Union((the_geom))) FROM ways WHERE id in
(SELECT edge FROM pgr_dijkstra(
‘SELECT id,
source,
target,
length AS cost
FROM ways’,
(SELECT id FROM ways_vertices_pgr
ORDER BY the_geom <-> ST_SetSRID(ST_Point(32.824, 39.937), 4326) LIMIT 1),
(SELECT id FROM ways_vertices_pgr
ORDER BY the_geom <-> ST_SetSRID(ST_Point(32.823, 39.934), 4326) LIMIT 1),
directed := true) foo);
{“type”:”MultiLineString”,”coordinates”:[[[32.8236785,39.9328593],[32.8236427,39.9331657],[32.8235632,39.9338447]],[[32.8239203,39.9365802],[32.8242237,39.9362869],[32.824926,39.9355603],[32.8254992,39.9350228],[32.8258432,39.9347217]],[[32.824946,39.932861],[32.8247683,39.9328783],[32.8237783,39.9328717],[32.8236785,39.9328593]],[[32.8258432,39.9347217],[32.8256654,39.9346335],[32.8252273,39.9343465],[32.8250696,39.9339926],[32.8250301,39.9336153],[32.8250189,39.9335673],[32.8249706,39.9330756],[32.824946,39.932861]]]}
Validating query result using geojson.io

Creating a Backend Server That Returns Routing Result

server.js
node_modules
.env
package.json
package-lock.json
server
├── routes
│ ├── api.router.js
├── db
│ ├── index.js
├── controllers
│ ├── api.routing.controller.js
server.js
.env file for db connection and port management
api.router.js
index.js for db connection
api.routing.controller.js

Testing the Server

Postman result

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store