Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
const http = require('http');
const https = require('https');
const fs = require('fs');
const port = process.env.PORT || 3000;

const azureStorageUrl = 'https://storagejoeri.blob.core.windows.net/dgjoeri/waardes.csv';

const server = http.createServer((req, res) => {
res.statusCode = 200;
const msg = 'Hello Node!\n'
res.end(msg);
if (req.method === 'GET') {
// Fetch and display the contents of waardes.csv
https.get(azureStorageUrl, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});

response.on('end', () => {
const lines = data.trim().split('\n');
const values = lines.map((line) => {
const columns = line.split(';');
return {
date: columns[0],
value: columns[1],
};
});

res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.write('<html><body>');

// Create a canvas for the chart
res.write('<canvas id="myChart" width="400" height="200"></canvas>');

// Generate the chart using Chart.js
res.write('<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>');
res.write('<script>');
res.write('var ctx = document.getElementById("myChart").getContext("2d");');
res.write('var labels = ' + JSON.stringify(values.map((line) => line.date)) + ';');
res.write('var data = ' + JSON.stringify(values.map((line) => line.value)) + ';');
res.write('var myChart = new Chart(ctx, {');
res.write('type: "line",');
res.write('data: {');
res.write('labels: labels,');
res.write('datasets: [{');
res.write('label: "Values",');
res.write('data: data,');
res.write('backgroundColor: "rgba(75, 192, 192, 0.2)",');
res.write('borderColor: "rgba(75, 192, 192, 1)",');
res.write('borderWidth: 1');
res.write('}]');
res.write('},');
res.write('options: {');
res.write('scales: {');
res.write('y: {');
res.write('beginAtZero: true');
res.write('}');
res.write('}');
res.write('}');
res.write('});');
res.write('</script>');

res.write('</body></html>');
res.end();
});
});
}
});

server.listen(port, () => {
Expand Down