-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticRouter.js
73 lines (55 loc) · 1.7 KB
/
staticRouter.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
65
66
67
68
69
70
71
72
#!/usr/bin/env node
"use strict";
const {
stat,
createReadStream,
} = require("fs");
const {
join,
extname
} = require("path");
// const { promisify } = require("util");
const Router = require("koa-router");
// const serve = require("koa-static");
// const authorize = require(`${__dirname}/middleware/authorize`);
// const session = require(`${__dirname}/middleware/session`);
const adminAuth = require(`${__dirname}/middleware/adminAuth`);
// const userAuth = require(`${__dirname}/middleware/userAuth`);
// const __readFile = promisify(readFile);
const staticRouter = new Router();
module.exports = staticRouter;
staticRouter.use(`/admin`, adminAuth());
staticRouter.get(`/:pageTemplate`, pageTemplateRoute);
staticRouter.get(`/admin/:pageTemplate`, adminPageTemplateRoute);
staticRouter.get("/user/:id", userController);
async function pageTemplateRoute (ctx, next) {
// ctx.params has pageTemplate
// this.state.title = ctx.params.pageTemplate;
this.render("default", { title: ctx.params.pageTemplate });
}
async function staticAssetRoute (ctx, next) {
// ctx.params has assetName
console.log("ctx state???", ctx.state);
const filePath = join(`${__dirname}/${ctx.params.assetPath}`)
// const filePath = join(`${__dirname}/public/index.html`);
const fstat = await _stat(filePath);
if (fstat.isFile()) {
ctx.type = extname(filePath);
ctx.body = createReadStream(filePath);
} else {
ctx.status = 404;
ctx.body = "Not found";
}
}
async function adminPageTemplateRoute (ctx, next) {
}
async function userController (ctx, next) {
}
function _stat (path) {
return new Promise((resolve, reject) => {
stat(path, (err, res) => {
if (err) return reject(err);
else return resolve(res);
});
});
}