forked from mendix/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (30 loc) · 930 Bytes
/
Copy pathserver.js
File metadata and controls
33 lines (30 loc) · 930 Bytes
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
const Server = require('spa-server');
const path = require('path');
const url = require('url');
const shell = require('shelljs');
const spawnServer = (folder) => {
const targetFolder = path.resolve(folder, '_site');
const server = Server.create({
path: targetFolder,
port: 8888,
middleware: [function (req, res, next) {
next();
}],
fallback: (req, res) => {
const parsed = url.parse(req.url),
target = parsed.pathname + '.html',
alternateTarget = parsed.pathname + '/index.html';
if (shell.test('-f', path.join(targetFolder, target))) {
return target + (parsed.query ? `?${parsed.query}` : '');
}
if (shell.test('-f', path.resolve(targetFolder, alternateTarget))) {
return alternateTarget + (parsed.query ? `?${parsed.query}` : '');
}
return '404.html';
}
})
server.start();
};
module.exports = {
spawn: spawnServer
};