forked from nathanhoad/nathanhoad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (43 loc) · 1.38 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
const fs = require('fs-extra');
const chalk = require('chalk');
const compile = require('./compiler');
const config = require('./config');
/**
* Run a web serve to serve out the static files
*/
function serve() {
const express = require('express');
const helmet = require('helmet');
const compression = require('compression');
const app = express();
app.use(helmet());
app.use(compression());
app.use(express.static(config.PUBLIC_PATH));
// Redirect 404s
app.get('*', (request, reply) => {
reply.redirect('/');
});
// Start the server
const port = process.env.PORT || 3000;
let listener = app.listen(port, () => {
console.log(chalk.bold.green(`Listening on port ${port}`));
});
// Enable auto restarting in development
if (process.env.NODE_ENV !== 'production') {
const enableDestroy = require('server-destroy');
enableDestroy(listener);
// Recompile on changes to anything
[config.POSTS_PATH, config.TEMPLATES_PATH].forEach(path => {
fs.watch(path, { recursive: true }, async (event, filename) => {
// Recompile posts
await compile();
// Purge all connections and close the server
listener.destroy();
listener = app.listen(port);
// Set up connection tracking so that we can destroy the server when a file changes
enableDestroy(listener);
});
});
}
}
compile().then(serve);