forked from outstack/pipeprint-engine-mjml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (51 loc) · 1.42 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Require the framework and instantiate it
const fastify = require('fastify')();
const mjml = require('mjml');
process.on('SIGINT', function() {
process.exit();
});
fastify.post('/render', { bodyLimit: 10485760 }, function (request, reply) {
try {
let template = request.body.files[request.body.template];
let rendered;
try {
rendered = mjml(template).html;
} catch (e) {
console.log(e);
reply
.code(400)
.type('application/problem+json')
.send(
JSON.stringify(
{
title: "MJML Error",
detail: e.message
}
)
);
return;
}
reply
.code(200)
.type('text/html')
.send(rendered);
} catch (e) {
console.log(e);
reply
.code(500)
.type('application/problem+json')
.send(
JSON.stringify(
{
title: "Server Error"
// detail: e.message
}
)
);
}
});
// Run the server!
fastify.listen(80, '0.0.0.0', function (err) {
if (err) throw err;
console.log(`server listening on ${fastify.server.address().port}`)
});