-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (24 loc) · 834 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const express = require('express');
const app = express();
const path = require('path');
const puppeteerToPdf = require('./src/puppeteer-to-pdf');
const port = process.env.PORT || 80;
app.use(express.static(path.join(__dirname, '/build')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/build/index.html'));
});
app.get('/getPdf', async (req, res) => {
const { url } = req.query;
const pdf = await puppeteerToPdf({ url });
res.setHeader('Access-Control-Allow-Origin', '*');
if (pdf) {
res.contentType('application/pdf');
res.send(pdf);
} else {
res.contentType('application/json');
res.status(422).send({
message: 'Cannot get your page',
});
}
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));