use promises

This commit is contained in:
Thomas Ruoff
2017-02-24 21:17:10 +01:00
parent 722accbe5d
commit f51daddc65
3 changed files with 48 additions and 58 deletions

View File

@@ -10,28 +10,19 @@ app.use(bodyParser.json());
app.options('/api/pdf/generate/:template');
app.post('/api/pdf/generate/:template', (req, res) => {
const templateName = req.params.template;
const options = req.body;
templates.get(templateName, options, (err, texDocument) => {
if (err) {
console.error('Error:', err.code, 'for', req.url);
res.sendStatus(500).end('Something went wrong while generating Tex source');
return;
}
renderer(texDocument, (err, pdfFilePath) => {
if (err) {
console.error('Error:', err.code, 'for', req.url);
res.sendStatus(500).end('Something went wrong while baking the PDF');
return;
}
const id = pdfFilePath.replace('/tmp/', '');
templates.get(templateName, options)
.then(renderer)
.then(id => {
res.send({id: id});
res.end();
})
.catch((err) => {
console.error('Error:', err, 'for', req.url);
res.sendStatus(500).end(err);
});
});
});
app.options('/api/pdf/:id');