-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 1.71 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
'use strict';
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const port = process.env.PORT || 3000;
const DocumentMerger = require('document-merger');
const info = require('./package');
const winston = require('winston');
const expressWinston = require('express-winston');
app.use(bodyParser.json({ limit: '50mb'}));
app.use(expressWinston.logger({
transports: [
new winston.transports.Console()
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
)
}));
app.post('/merge', (req, res) => {
if (!req.body) {
return res.status(400).send('no or invalid body given');
}
if (!req.body instanceof Object && !req.body instanceof Array) {
return res.status(400).send('payload must be either an object or array of documents');
}
if (!Array.isArray(req.body) && !req.body.documents) {
return res.status(400).send('documents property must be given if payload is an object');
}
if (req.body.documents && !Array.isArray(req.body.documents)) {
return res.status(400).send('documents must be an array');
}
const html = req.body.documents || req.body;
const documents = html
.filter((doc) => typeof doc === 'string')
.map((doc) => ({ content: doc, type: 'html' }));
const options = { documents };
if (!Array.isArray(req.body)) {
delete req.body.documents;
Object.assign(options, req.body);
}
const merger = new DocumentMerger();
res.status(200).send(merger.merge(options));
});
app.get('/', (req, res) => {
res.json({
name: info.name,
description: info.description,
version: info.version
});
});
app.listen(port, () => {
console.log(`listening on port ${port}`);
});