Deutsche Bahn opened some of their APIs for public consumption and this project should serve as an API-playground.
For more detailed info on the API itself read this article (in German)
The Homepage of the API is located here.
The API Descriptions can be found here (pdf, in German)
The Backend is based on Hapi.js which is my favorite node server. The Frontend uses this backend to avoid problems with CORS. Therefore, the App executes no direct calls against the real DB-API but instead against Hapi.js that is configured to accept CORS.
There's a special class called BahnService* that's being used by both client and server to make API calls. BahnService runs either in local or remote mode. The Client runs on local to dispatch the API calls to the local Hapi.js instance which in turn uses the remote instance of BahnService to access the real DB-API.
This may sound unnecessarily complex but the strategy is rather simple: a Client only wants to have a direct access to 'some API' without fiddling around with CORS etc. Therefore, Hapi.js takes care of providing us an easy to use, RESTful API that returns plain JSONs.
The advantage is obvious: you can mock and test your API without dealing with CORS or any other implementation details.
For example, the client could send a GET request querying the location 'Cologne':
which on Hapi.js side would then be translated into a server-side GET request:
Finally, you'd receive a plain JSON-structure containing the location data. The JSON-structure remains unchanged as Hapi.js only forwards the responses back to the client.
Local API Server
The Client is rather primitive as it only contains a very bare-bones React component and a few Bootstrap elements (button, input, panel etc.).
The Calendar Widgets are from React Widgets.
The Server implements only the Location.name API but soon they'll be more of them.
Building & Running
npm run client
Client is located at http://localhost:8080/
npm run api
API-Server listens on http://localhost:3000/
More info on testing ES6 code with Mocha here.
- What do 'BahnService' and 'OpenLok' mean?
Bahn is a German word and heavily context-dependent. In this case it means 'Train'.
However, you can find it in a number of different locations, for example:
- Autobahn = motorway
- Flugbahn = trajectory
- Straßenbahn = tram (Br.), streetcar (Am.)
and many others.
Lok is an abbreviation of 'Lokomotive' which means
There's also a similar English abbreviation
Building compound words is very easy in German.
You can learn it here, for example
- Where to get the API-Key?
To access the DB-API you'll need an API-Key. To get one use this email: dbopendata @ deutschebahn.com
Then put it into authKey in BahnService.js
Deutsche Bahn® is a registered trademark of Deutsche Bahn AG in Germany and/or other countries.