tracktracker/server.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-01-13 11:20:35 +00:00
const express = require('express');
const request = require('request');
const csv = require('csv-parse');
2020-01-13 11:39:28 +00:00
const data = require('./back/data');
2020-01-13 11:20:35 +00:00
const app = express();
const port = 3000;
2020-01-13 11:39:28 +00:00
app.use(express.static('dist'));
2020-01-13 11:20:35 +00:00
const TAM_REALTIME = 'http://data.montpellier3m.fr/node/10732/download';
app.get('/realtime', (req, res) =>
{
const parser = csv({
delimiter: ';',
});
passages = {};
passagesLast = Date.now();
const stream = request(TAM_ENDPOINT)
.pipe(parser);
stream.on('readable', () =>
{
let row;
while (row = stream.read())
{
if (row.length === 0 || row[0] === 'course')
{
// Ignore les lignes invalides
continue;
}
if (passages[row[2]] === undefined)
{
passages[row[2]] = [];
}
passages[row[2]].push({
ligne: row[4],
destination: row[5],
direction: row[6],
time: row[7],
theorique: row[8],
});
}
});
stream.on('end', () => res());
stream.on('error', err => rej(err));
res.send('Hello World!');
})
app.get('/line/:lineRef', async (req, res) =>
{
const lineData = await data.fetchLineData(req.params.lineRef);
res.json(lineData);
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`))