-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
server.js
executable file
·64 lines (49 loc) · 1.66 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
59
60
61
62
63
64
#!/usr/bin/env node
/* eslint-disable max-len */
'use strict';
const pkg = require('./package.json');
const http = require('http');
const finalhandler = require('finalhandler');
const getPort = require('get-port');
const {dirname, sep, extname, basename} = require('path');
const {PORT:port=3000, HOST:host='127.0.0.1'} = process.env;
/*
Static files handling
*/
const serveStatic = require('serve-static');
const staticDist = serveStatic('./dist');
const staticRoot = serveStatic('./');
/*
Server definition
*/
const server = http.createServer((request, response) => {
response.setHeader('X-Content-Type-Options', 'nosniff');
response.setHeader('X-Robots-Tag', 'noindex, nofollow, noarchive');
staticDist(request, response, () => {
staticRoot(request, response, finalhandler(request, response));
});
});
const startServer = (port) => {
const url = `http://${host}:${port}`;
return server.listen(port, host, () => {
console.log('💡 %s@%s consultable sur %s', pkg.name, pkg.version, url);
});
}
module.exports = startServer;
if (require.main === module) {
/* eslint-disable global-require */
const chokidar = require('chokidar');
const build = require('./bin/build')('html5', '.html');
const rebuild = (path) => {
const source = extname(path) === '.adoc' ? basename(path): 'index.adoc';
const file = `${dirname(path).split(sep).shift()}/${source}`;
build(file);
};
// effectively start the server
getPort({ port, host }).then(startServer);
// check for file changes
chokidar.watch(['**/*.adoc', '*/examples/**/*'], { ignoreInitial: true })
.on('add', rebuild)
.on('change', rebuild)
.on('error', console.error);
}