protospace-lcars/server.js

35 lines
846 B
JavaScript

const request = require('request');
const cheerio = require('cheerio');
const express = require('express')
const app = express()
app.use('/', express.static('dist'))
app.get('/hello', (req, res) => {
res.send('Hello World!')
})
app.get('/api/data/classes', (req, res) => {
request('https://my.protospace.ca/school', (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
let classes = [];
$('table.grid tbody tr').each((i, elem) => {
classes[i] = {
subject: $(elem).children().eq(0).text(),
date: $(elem).children().eq(2).text(),
available: $(elem).children().eq(6).text()
}
});
res.setHeader('Content-Type', 'application/json');
res.send(classes);
}
});
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})