-
Notifications
You must be signed in to change notification settings - Fork 144
/
serve.js
54 lines (48 loc) · 1.42 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
46
47
48
49
50
51
52
53
54
//
// This script will run a local development server. This is useful when
// developing the theme.
//
// Usage:
// `node serve`
//
const http = require('http');
const fs = require('fs');
const resume = JSON.parse(fs.readFileSync('node_modules/resume-schema/sample.resume.json', 'utf8'));
const theme = require('./index.js');
const path = require('path');
const PORT = 8888;
http.createServer(function(req, res) {
const picture = resume.basics.picture && resume.basics.picture.replace(/^\//, '');
if (picture && req.url.replace(/^\//, '') === picture.replace(/^.\//, '')) {
const format = path.extname(picture);
try {
const 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:${PORT}/`);
console.log('Serving..');
function render() {
try {
return theme.render(JSON.parse(JSON.stringify(resume)));
} catch (e) {
console.log(e.message);
return '';
}
}