100 lines
4.2 KiB
JavaScript
100 lines
4.2 KiB
JavaScript
import express from 'express';
|
|
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';
|
|
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));
|
|
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 = 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 = 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 = req.query;
|
|
let 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 => {
|
|
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 => 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}`);
|
|
});
|
|
//# sourceMappingURL=app.js.map
|