-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
42 lines (37 loc) · 1.34 KB
/
server.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
import {
Application,
Router,
RouterContext,
} from 'https://deno.land/x/oak@v10.4.0/mod.ts';
import { parse } from "https://deno.land/std@0.130.0/flags/mod.ts";
import { oakCors } from "https://deno.land/x/cors/mod.ts";
import { getJOYtoys, insertJOYtoy } from './db.ts';
import newJOYtoy from "./db.ts";
const app = new Application();
const router = new Router();
const PORT = 8000;
router.put("/api/put", async ({request, response}: RouterContext<any>) => {
try {
const newToy:newJOYtoy = await request.body().value
const data = await insertJOYtoy(newToy)
data === undefined
? response.body = { message: "Success", data: "Something isn't right", status: 200 }
: response.body = { message: "Success", data: data, status: 200 }
}catch(e){
response.body = { message: `Something went wrong : ${e}`, status: 500 }
}
})
router.get("/api/get", async ({response}: RouterContext<any>) => {
try {
const data = await getJOYtoys()
response.body = { message: "Success", data: data, status: 200 }
} catch(e) {
response.body = { message: `Something went wrong : ${e}`, status: 500 }
}
})
app.use(oakCors())
app.use(router.routes())
app.use(router.allowedMethods())
const argPort = parse(Deno.args).port;
console.log(`***LISTENING ON PORT: ${argPort ? argPort : PORT}***`)
await app.listen({ port: argPort ?? PORT });