-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
47 lines (37 loc) · 976 Bytes
/
app.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
import {Application, log, send} from "./deps.js";
import router from "./routes/router.js";
const app = new Application();
// logging request
app.use(async (ctx, next) => {
log.info(`${ctx.request.method}: ${ctx.request.url}`);
await next();
})
// routing
app.use(router.routes());
app.use(router.allowedMethods());
// serve index.html
router.get('/', async (ctx, next) => {
await send(ctx, "/index.html", {
root: `${Deno.cwd()}/static`
})
log.info("Serve index.html successfully");
})
// serve other files in the static folder
app.use(async (ctx, next) => {
const filePath = ctx.request.url.pathname;
// regex to find .js .css in filePath
let reg = /.css|.js$/;
const result = filePath.match(reg);
if(result) {
// send js css file
await send(ctx, filePath, {
root: `${Deno.cwd()}/static`
})
} else {
// send index.js
await send(ctx, "/index.html", {
root: `${Deno.cwd()}/static`
})
}
})
export default app;