bahnui/back/src/app.ts

35 lines
1.2 KiB
TypeScript

import express from 'express';
import {createClient} from 'hafas-client'
import {profile as dbProfile} from 'hafas-client/p/db/index.js'
const app = express();
const port = 3000;
// Adapt this to your project! createClient() won't work with this string.
const userAgent = 'sperwing@sperwing.de'
// create a client with the Deutsche Bahn profile
const client = createClient(dbProfile, userAgent)
app.use((req, res, next) => {
res.append('Access-Control-Allow-Origin', ['*']);
res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.append('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.get('/', (req, res) => {
client.journeys('8011167', '8000261', {results: 1}).then(result => res.send(result));
});
app.get('/searchStation', (req, res) => {
let query: any = req.query;
client.locations(query.text, {results:1}).then(result => res.send({name: result[0].name, id:result[0].id}));
});
app.get('/searchConnection', (req, res) => {
let query: any = req.query;
client.journeys(query.from, query.to,{results:5}).then(result => res.send(result.journeys)).catch(error => res.sendStatus(500));
});
app.listen(port, () => {
return console.log(`Express is listening at http://localhost:${port}`);
});