76 lines
3.1 KiB
TypeScript
76 lines
3.1 KiB
TypeScript
import express from 'express';
|
|
import {createClient, HafasClient} from 'hafas-client'
|
|
import {profile as dbProfile} from 'hafas-client/p/db/index.js'
|
|
import {profile as vbbProfile} from 'hafas-client/p/vbb/index.js'
|
|
import {profile as pkpProfile} from 'hafas-client/p/pkp/index.js'
|
|
import {profile as irishProfile} from 'hafas-client/p/irish-rail/index.js'
|
|
import {profile as oebbProfile} from 'hafas-client/p/oebb/index.js'
|
|
import {profile as luProfile} from 'hafas-client/p/mobiliteit-lu/index.js'
|
|
import {profile as zvvProfile} from 'hafas-client/p/zvv/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)
|
|
let clients = new Map<string, HafasClient>();
|
|
clients.set("db", client);
|
|
clients.set("vbb", createClient(vbbProfile, userAgent));
|
|
clients.set("zvv", createClient(zvvProfile, userAgent));
|
|
clients.set("pkp", createClient(pkpProfile, userAgent));
|
|
clients.set("irish", createClient(irishProfile, userAgent));
|
|
clients.set("oebb", createClient(oebbProfile, userAgent));
|
|
clients.set("lu", createClient(luProfile, 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)).catch(error => res.status(500).send(error));
|
|
});
|
|
app.get('/searchStation', (req, res) => {
|
|
if(!req.query || !req.query.text){
|
|
res.send([]);
|
|
} else {
|
|
let query: any = req.query;
|
|
clients.get((String) (req.query.service)).locations(query.text, {results:1}).then(result => res.send({name: result[0].name, id:result[0].id})).catch(error => res.status(500).send(error));
|
|
}
|
|
});
|
|
app.get('/searchStations', (req, res) => {
|
|
console.log(req)
|
|
if(!req.query || !req.query.text){
|
|
res.send([]);
|
|
} else {
|
|
let query: any = req.query;
|
|
try {
|
|
clients.get((String) (req.query.service)).locations(query.text, {results:6}).then(result => res.send(result.map(entry => {return {name:entry.name, id:entry.id}}))).catch(error => res.status(500).send(error));
|
|
} catch (e) {
|
|
res.status(500).send(e)
|
|
}
|
|
}
|
|
});
|
|
app.get('/searchConnection', (req, res) => {
|
|
if(!req.query || !req.query.from || !req.query.to || !req.query.date || !req.query.service){
|
|
console.log(req.query);
|
|
res.send([]);
|
|
} else {
|
|
let query: any = req.query;
|
|
let date: Date = new Date((String)(req.query.date));
|
|
try {
|
|
clients.get((String) (req.query.service)).journeys(query.from, query.to,{results:5, departure:date}).then(result => res.send(result.journeys)).catch(error => res.status(500).send(error));
|
|
} catch (e) {
|
|
res.status(500).send(e)
|
|
}
|
|
}
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
return console.log(`Express is listening at http://localhost:${port}`);
|
|
});
|