-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (31 loc) · 1.31 KB
/
server.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
32
33
34
35
36
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const cors = require('cors')
const prerender = require('./src/prerender/prerender').prerender;
// configure express
const app = express();
app.use(cors())
app.use(bodyParser.json());
app.get('/', (req, res, next) => {
res.sendFile(path.join(__dirname, 'views/index.html'));
});
app.get('/api/parse', async (req, res) => {
const url = req.query['url'];
const {html, meta, ttRenderMs} = await prerender(url);
// Add Server-Timing! See https://w3c.github.io/server-timing/.
res.set('Server-Timing', `Prerender;dur=${ttRenderMs};desc="Headless render time (ms)"`);
return res.status(200).json(meta); // Serve prerendered page as response.
});
app.get('/api/prerender', async (req, res) => {
const url = req.query['url'];
const {html, meta, ttRenderMs} = await prerender(url);
// Add Server-Timing! See https://w3c.github.io/server-timing/.
res.set('Server-Timing', `Prerender;dur=${ttRenderMs};desc="Headless render time (ms)"`);
return res.status(200).send(html).end(); // Serve prerendered page as response.
});
const server = app.listen(process.env.PORT || 8080, '0.0.0.0', () => {
console.log('App listening at http://%s:%s',
server.address().address,
server.address().port);
});