-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (60 loc) · 1.89 KB
/
server.js
File metadata and controls
71 lines (60 loc) · 1.89 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {
Application,
Router,
send
} from "./dependencies.js";
import { multiParser } from "./dependencies.js";
import {
viewEngine,
engineFactory,
adapterFactory
} from "./dependencies.js";
const ejsEngine = await engineFactory.getEjsEngine();
const oakAdapter = await adapterFactory.getOakAdapter();
const app = new Application();
app.use(viewEngine(oakAdapter,ejsEngine));
app.use(async (ctx,next) => {
await send(ctx, ctx.request.url.pathname,{
root: `${Deno.cwd()}/static`
});
next();
});
const router = new Router();
router
.get("/", async (context) => {
context.render(`${Deno.cwd()}/views/index.ejs`);
})
.get("/register", async (context) => {
// context.response.body = await renderFileToString(
// `${Deno.cwd()}/views/pages/authorization/registration.ejs`
// );
})
.post("/register", async (context) => {
const form = JSON.stringify(await multiParser(context.request.serverRequest));
const parse = JSON.parse(form);
console.log(parse["fields"]["username"]);
context.response.redirect("/")
})
.get("/login", async (context) => {
// context.response.body = await renderFileToString(
// `${Deno.cwd()}/views/pages/authorization/login.ejs`
// );
})
.post("/login", async (context) => {
const form = JSON.stringify(await multiParser(context.request.serverRequest));
const parse = JSON.parse(form);
console.log(parse["fields"]["username"]);
context.response.redirect("/")
});
/*
When I enable lines below it gives an error when I try to enable static files.
When I disable no error and pages work too.
I think it's a bug of oak.
*/
// app.addEventListener('error', event => {
// console.log(event.error);
// });
app.use(router.routes());
app.use(router.allowedMethods());
console.log("Server is running 🦕")
await app.listen({ port: 8000 });