-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (40 loc) · 1.51 KB
/
server.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
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import API from './api/index.ts'
const port = 8080;
const apiHandler =(API, urlPaths,data) => {
let apiCall = API[urlPaths[1]]['index'];
const hasFunction = urlPaths.length > 2
//
if(hasFunction)
apiCall = API[urlPaths[1]][urlPaths[2]];
return data ? apiCall(data) :apiCall();
}
const handler = async (request) => {
let response;
try{
//need to add support for being able to handle the base path
// for adding support for authorization
// console.log('headers',request.headers.get('authorization'));
let url = new URL(request.url)
let urlPaths = url.pathname.split('/')
if(request.method == 'GET'){
response = Response.json(apiHandler(API,urlPaths));
// response = new Response(JSON.stringify()), {
// headers:{
// "content-type": "application/json"
// },
// status: 200 });
}else if(request.method == 'POST'){
const data = await request.json()
response = Response.json(apiHandler(API,urlPaths,data))
}
}catch(err){
// look into support for logging service or build own
return Response.json({status:'error', msg:err.message},{
status: 500
});
}
return response;
};
console.log(`HTTP webserver running. Access it at: http://localhost:${port}/`);
await serve(handler, { port });