134 lines
5.8 KiB
JavaScript
134 lines
5.8 KiB
JavaScript
import express from 'express';
|
|
import bodyParser from 'body-parser';
|
|
import { createClient } 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';
|
|
import { profile as bartProfile } from 'hafas-client/p/bart/index.js';
|
|
import { profile as dartProfile } from 'hafas-client/p/dart/index.js';
|
|
import { profile as nrwProfile } from 'hafas-client/p/mobil-nrw/index.js';
|
|
import { profile as danmarkProfile } from 'hafas-client/p/rejseplanen/index.js';
|
|
import { profile as blsProfile } from 'hafas-client/p/bls/index.js';
|
|
import { profile as nrwbusradarProfile } from 'hafas-client/p/db-busradar-nrw/index.js';
|
|
import { profile as cmtaProfile } from 'hafas-client/p/cmta/index.js';
|
|
import { profile as vrnProfile } from 'hafas-client/p/vrn/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();
|
|
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));
|
|
clients.set("bart", createClient(bartProfile, userAgent));
|
|
clients.set("dart", createClient(dartProfile, userAgent));
|
|
clients.set("nrw", createClient(nrwProfile, userAgent));
|
|
clients.set("danmark", createClient(danmarkProfile, userAgent));
|
|
clients.set("bls", createClient(blsProfile, userAgent));
|
|
clients.set("nrwbus", createClient(nrwbusradarProfile, userAgent));
|
|
clients.set("cmta", createClient(cmtaProfile, userAgent));
|
|
clients.set("vrn", createClient(vrnProfile, 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.use(bodyParser.json());
|
|
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 = req.query;
|
|
clients.get((String)(req.query.service)).locations(query.text, { results: 1, subStops: false, entrances: false, language: query.language }).then(result => res.send(result[0])).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 = req.query;
|
|
try {
|
|
clients.get((String)(req.query.service)).locations(query.text, { results: 6, subStops: false, entrances: false, language: query.language }).then(result => res.send(result)).catch(error => res.status(500).send(error));
|
|
}
|
|
catch (e) {
|
|
res.status(500).send(e);
|
|
}
|
|
}
|
|
});
|
|
app.post('/searchConnection', (req, res) => {
|
|
if (!req.body.data || !req.body.data.from || !req.body.data.to || !req.body.data.date || !req.body.data.service) {
|
|
res.send([]);
|
|
}
|
|
else {
|
|
let query = req.body.data;
|
|
let date = new Date((String)(query.date));
|
|
try {
|
|
console.log(query.language);
|
|
clients.get((String)(query.service)).journeys(query.from, query.to, { results: 5, departure: date, language: query.language })
|
|
.then(result => {
|
|
let journeys = result.journeys;
|
|
res.send(journeys.map(journey => {
|
|
return {
|
|
legs: journey.legs.filter(leg => leg.origin.id !== leg.destination.id),
|
|
remarks: journey.remarks,
|
|
type: journey.type,
|
|
cycle: journey.cycle,
|
|
price: journey.price,
|
|
};
|
|
}));
|
|
})
|
|
.catch(error => {
|
|
console.log(error);
|
|
res.status(500).send(error);
|
|
});
|
|
}
|
|
catch (e) {
|
|
res.status(500).send(e);
|
|
}
|
|
}
|
|
});
|
|
app.get('/findNearby', (req, res) => {
|
|
if (!req.query || !req.query.location) {
|
|
res.send([]);
|
|
}
|
|
else {
|
|
let query = req.query;
|
|
try {
|
|
clients.get((String)(req.query.service)).nearby({ type: "location", latitude: parseFloat(query.location.latitude), longitude: parseFloat(query.location.longitude) }, { subStops: false, entrances: false, language: query.language }).then(result => res.send(result)).catch(error => {
|
|
console.log(error);
|
|
res.status(500).send(error);
|
|
});
|
|
}
|
|
catch (e) {
|
|
res.status(500).send(e);
|
|
}
|
|
}
|
|
});
|
|
app.get('/nearbyTest', (req, res) => {
|
|
client.nearby({
|
|
type: 'location',
|
|
latitude: 52.5137344,
|
|
longitude: 13.4744798
|
|
}, undefined).then(result => res.send(result)).catch(error => res.status(500).send(error));
|
|
});
|
|
app.listen(port, () => {
|
|
return console.log(`Express is listening at http://localhost:${port}`);
|
|
});
|
|
//# sourceMappingURL=app.js.map
|