@ima-worldhealth/sunfish
Version:
A webapp for configuring DHIS2 email reports
31 lines (24 loc) • 727 B
JavaScript
const router = require('express').Router();
const dayjs = require('dayjs');
const executor = require('../lib/executor');
router.get('/schedule', (req, res) => {
res.writeHead(200, {
'Content-Type' : 'text/event-stream',
'Cache-Control' : 'no-cache',
Connection : 'keep-alive',
'X-Accel-Buffering' : 'no',
});
res.write('\n');
// listen for active events and fire them off
executor.events.on('event', (message) => {
const timestamp = dayjs().format('YYYY-MM-DD HH:mm:ss');
const datapacket = {
timestamp,
message,
};
res.write(`id: ${Date.now()}\n`);
res.write(`data: ${JSON.stringify(datapacket)}\n\n`);
res.flushHeaders();
});
});
module.exports = router;