Hey @aadams you can indeed use JsonRoutes (one of the simple:rest packages) to write to a stream and send that as a response or anything you want because you have access to the response object, the res
part of function(req, res) {}
signature.
import { JsonRoutes } from 'meteor/simple:json-routes';
import Excel from 'exceljs';
JsonRoutes.add('get', '/downloadExcel', (req, res) => {
JsonRoutes.setResponseHeaders({
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition': 'attachment;',
});
const wb = new Excel.Workbook();
wb.addWorksheet(`sheet one`);
wb
.xlsx
.write(res)
.then(() => JsonRoutes.sendResult(res));
})
I’m glad you found simple:rest
easy because it is and it is also very powerful. I doubt you’d have much trouble with it, if at all.
As for using connect without express, an example would be:
import { WebApp } from 'meteor/webapp';
import connectRoute from 'connect-route';
import bodyParser from 'body-parser';
const app = WebApp.connectHandlers;
app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
app.use(bodyParser.json({ limit: '50mb' }));
app.use((req, res, next) => {
console.log('New request accepted', req);
next();
});
app.use((req, res, next) => {
res.setHeader('Content-type', 'application/json');
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With');
next();
});
app.use(Meteor.bindEnvironment(connectRoute((router) => {
router.get('/say/:what', (req, res) => {
const {
what,
} = req.params;
const response = {
say: what,
};
const prettyResponse = JSON.stringify({
status: 'success',
response,
}, null, 2)
res.end(prettyResponse);
});
})));
Don’t be alarmed by the lines of code, I just wanted to demonstrate to you that you can do anything.
The main difference compared to express is that connect does not have a router out of box, so that’s what I added. Express comes loaded with other stuff like a view/templating engine, which I doubt you’d ever need since all you want is a rest api. Express is more relevant for “web apps” whereas I found Connect capable enough for “rest apis”.