-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
40 lines (36 loc) · 1.28 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
"use strict";
var http = require("http");
var wkhtmltopdf = require('wkhtmltopdf');
var qs = require('querystring');
http.createServer(function (request, response) {
if (request.method === 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
var post = qs.parse(body),
html = post.html;
if (html !== undefined) {
response.writeHead(200, {
'Content-Type': 'application/pdf',
});
delete post.html;
wkhtmltopdf(html, post, function (code) {
if (code !== null) {
response.writeHead(500, "Internal Server Error", {'Content-Type': 'text/html'});
response.end();
console.log(code);
}
}).pipe(response);
} else {
response.writeHead(400, "Bad Request", {'Content-Type': 'text/html'});
response.end();
}
});
} else {
response.writeHead(200, "OK", {'Content-Type': 'text/html'});
response.end();
}
}).listen(5001);
console.log("Server is running at http://127.0.0.1:5001");