-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (42 loc) · 1.21 KB
/
index.js
File metadata and controls
50 lines (42 loc) · 1.21 KB
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
const express = require("express")
const eta = require("eta")
const fs = require("fs")
const app = express()
const blocksFolder = './blocks'
app.engine("html", eta.renderFile)
app.engine("eta", eta.renderFile)
app.set("view engine", "html")
app.set("views", ["./view", "./view/layouts", "./blocks"])
app.use('/static', express.static('./public'))
function blocksWalk(dir) {
let results = [];
fs.readdirSync(dir).forEach(function (file) {
const path = dir + '/' + file;
const stat = fs.statSync(path);
if (stat && stat.isDirectory()) {
results = results.concat(blocksWalk(path));
} else {
const category = dir.replace('./blocks/', '');
const name = file.replace('.html', '');
results.push({
category: category,
name: name
});
}
});
return results;
}
app.get("/", function (req, res) {
const blocks = blocksWalk(blocksFolder)
res.render("_list_blocks", {
blocks: blocks
})
})
app.get("/blocks/:category/:block", function (req, res) {
res.render(req.params.category + "/" + req.params.block, {
title: req.params.category + "/" + req.params.block
})
})
app.listen(8080, function () {
console.log("Server started: http://127.0.0.1:8080")
})