-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
48 lines (37 loc) · 1.24 KB
/
app.ts
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
import { Application } from "https://deno.land/x/[email protected]/mod.ts";
import todosRoutes from "./routes/todo.ts";
import { connect } from "./utils/db_utils.ts";
import { config } from "https://deno.land/x/dotenv/mod.ts";
connect();
const app = new Application();
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
console.log(err);
ctx.response.body = {
message: "Your request could not be processed!",
};
ctx.response.status = 500;
}
});
app.use(async (ctx, next) => {
const responseHeaders = ctx.response.headers;
responseHeaders.set("Access-Control-Allow-Origin", "*");
responseHeaders.set(
"Access-Control-Allow-Methods",
"OPTIONS, GET, POST, PUT, DELETE"
);
responseHeaders.set("Access-Control-Allow-Headers", "Content-Type");
await next();
});
app.use(async (ctx, next) => {
console.log("This always runs!");
await next(); //if any of our other middlewares have async tasks we must put await for all middleware, otherwise
//a response will be sent before the async tasks are finsihed
});
app.use(todosRoutes.routes());
app.use(todosRoutes.allowedMethods());
const envConfig = await config();
const PORT = parseInt(Deno.env.get("PORT") as string);
await app.listen({ port: PORT });