mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 14:37:21 +01:00
77 lines
1.9 KiB
JavaScript
77 lines
1.9 KiB
JavaScript
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const app = express();
|
|
const templates = require('./templates');
|
|
const renderer = require('./renderer');
|
|
const store = require('./store');
|
|
const { getPdfPath } = require('./utils');
|
|
|
|
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)
|
|
.then(renderer)
|
|
.then(id => {
|
|
const storeData = Object.assign({}, options, {
|
|
id,
|
|
created: new Date().toISOString()
|
|
});
|
|
return store
|
|
.add(storeData)
|
|
.then(() => id);
|
|
})
|
|
.then(id => {
|
|
res
|
|
.status(200)
|
|
.json({id: id});
|
|
})
|
|
.catch((err) => {
|
|
console.error('Error:', err, 'for', req.url);
|
|
res
|
|
.status(500)
|
|
.json({error: err.toString()});
|
|
});
|
|
});
|
|
|
|
app.get('/api/pdf/latest', (req, res) => {
|
|
store
|
|
.list()
|
|
.then(results => res
|
|
.status(200)
|
|
.json(results)
|
|
)
|
|
.catch(err => res
|
|
.status(500)
|
|
.json({error: err.toString()})
|
|
);
|
|
});
|
|
|
|
app.delete('/api/pdf/latest/:id', (req, res) => {
|
|
const { id } = req.params;
|
|
console.log(`Deleting ${id}...`);
|
|
store
|
|
.remove(id)
|
|
.then(() => res.sendStatus(202))
|
|
.catch(err => res
|
|
.status(500)
|
|
.json({error: err.toString()})
|
|
);
|
|
});
|
|
|
|
|
|
app.options('/api/pdf/:id');
|
|
app.get('/api/pdf/:id', (req, res) => {
|
|
const {id} = req.params;
|
|
res
|
|
.status(200)
|
|
.sendFile(getPdfPath(id));
|
|
});
|
|
|
|
app.use(express.static('../client/build'));
|
|
|
|
app.listen(5000);
|