-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·42 lines (35 loc) · 912 Bytes
/
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
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
require('babel-core/register');
require('marko/node-require');
const Koa = require('koa');
const template = require('./views/index.marko');
const port = process.env.PORT || 9999;
module.exports = function Server(inspectedDir, mode) {
const app = new Koa();
// read list of files
const files = fs.readdirSync(inspectedDir);
app.use((ctx) => {
let data = null;
let file = '';
if (ctx.query.file) {
file = ctx.query.file;
const rawData = fs.readFileSync(
path.join(inspectedDir, ctx.query.file),
'utf8',
);
data = JSON.parse(rawData);
}
ctx.type = 'html';
ctx.body = template.stream({
file,
files,
data,
mode,
});
});
app.listen(port);
console.log(`Server is up. Visit http://localhost:${port}`);
return app;
};