-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.js
45 lines (41 loc) · 1.24 KB
/
serve.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
var http = require("http");
var fs = require("fs");
var resume = JSON.parse(fs.readFileSync('/src/resume.json', 'utf8'));
var theme = require("./index.js");
var path = require("path");
var port = 3000;
http.createServer(function(req, res) {
var picture = resume.basics.picture && resume.basics.picture.replace(/^\//, "");
if (picture && req.url.replace(/^\//, "") === picture.replace(/^.\//, "")) {
var format = path.extname(picture);
try {
var image = fs.readFileSync(picture);
res.writeHead(200, {
"Content-Type": "image/" + format
});
res.end(image, "binary");
} catch (error) {
if (error.code === "ENOENT") {
console.log("Picture not found !");
res.end();
} else {
throw error;
}
}
} else {
res.writeHead(200, {
"Content-Type": "text/html"
});
res.end(render());
}
}).listen(port);
console.log("Preview: http://localhost:3000/");
console.log("Serving..");
function render() {
try {
return theme.render(JSON.parse(JSON.stringify(resume)));
} catch (e) {
console.log(e.message);
return "";
}
}