-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
45 lines (39 loc) · 1.21 KB
/
app.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
37
38
39
40
41
42
43
44
45
const express = require('express')
const app = express()
const stringhash = require('string-hash')
const fs = require('fs')
const Readable = require('stream').Readable
const convert = require('html-convert')({
width : 600, // Note: This doesn't appear to work as per the docs
height : 200,
})
const bodyParser = require('body-parser')
const port = 8080
app.use(express.static('public'))
app.use(bodyParser());
app.post('/make-image', function (req, res) {
const html = req.body.html
const force = req.body.force
// Create a hash off the html
const hash = stringhash(html)
const filename = `${hash}.png`
const imagepath = `./public/${filename}`
// See if the image has already been created
if (force || !fs.existsSync(imagepath)) {
// Else, we'll generate an image from the html
const s = new Readable();
s._read = function noop() {}; // Actually required
s.push(html);
s.push(null);
const tmpFileStream = s.pipe(convert())
.pipe(fs.createWriteStream(imagepath))
tmpFileStream.on('finish', function () {
res.send(filename)
})
} else {
res.send(filename)
}
})
app.listen(port, function () {
console.log(`Listening on port: ${port}`)
})