-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
71 lines (63 loc) · 1.66 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express')
const mjml = require('mjml')
const { registerDependencies } = require('mjml-validator')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 80
const app = express()
/**
* Override the validation parameters by specifying that
* the mj-wrapper element CAN have another mj-wrapper as a child component.
*/
registerDependencies({
'mj-wrapper': ['mj-wrapper', 'mj-hero', 'mj-raw', 'mj-section'],
'mj-body': ['mj-raw', 'mj-section', 'mj-wrapper', 'mj-hero', 'mj-text']
})
app.use(bodyParser.text())
app.post('/', (req, res) => {
if (req.body) {
if (req.headers['content-type'] !== 'text/plain') {
res.type('json')
.status(400)
.send(JSON.stringify({
error: 'Only text/plain content-type is authorized.'
}))
return
}
/**
* Query params
*/
const comments = req.query.comments === 'true'
const beautify = req.query.beautify === 'true'
const minify = req.query.minify === 'true'
const validationLevel = req.query.validation || 'strict'
try {
const output = mjml(req.body, {
keepComments: comments,
beautify,
minify,
validationLevel
})
res
.type('html')
.status(200)
.send(output.html)
} catch (e) {
res
.type('json')
.status(422)
.send(JSON.stringify({
error: e.message
}))
}
} else {
res
.type('json')
.status(400)
.send(JSON.stringify({
error: 'Missing content on the body request.'
}))
}
})
app.listen(PORT, () => {
console.log(`MJML to HTML started on port ${PORT}`)
})