-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (68 loc) · 1.97 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const serverless = require("serverless-http");
const Koa = require("koa");
const route = require("koa-route");
const compress = require("koa-compress");
const { bodyParser } = require("@koa/bodyparser");
const app = (module.exports = new Koa());
app.use(compress());
app.use(bodyParser({
enableTypes: ['json', 'text']
}));
const API_CONFIG = {
baseURL: process.env.EXPRESS_OTEL_API_ENDPOINT
};
app.use(
route.get("/", async function (ctx) {
ctx.body = "Hello World";
})
);
app.use(
route.get("/path", async function (ctx) {
ctx.body = "Hello from path";
})
);
app.use(
route.post("/", async function (ctx) {
ctx.body = ctx.request.body || "Hello from POST";
})
);
app.use(
route.get("/weather", async function (ctx, next) {
var axios = require("axios");
var config = {
method: "get",
url: `${API_CONFIG.baseURL}/weather?location=${ctx.request.query.location}`,
headers: {
"Content-Type": "application/json",
},
};
console.log(`config: ${JSON.stringify(config)}`);
// var config = {
// method: "get",
// url: `${API_CONFIG.baseURL}/weather?q=${ctx.request.query.location}&appid=${API_CONFIG.TOKEN}`,
// headers: {
// "Content-Type": "application/json",
// },
// };
return await axios(config)
.then(function (response) {
console.info(
`${ctx.request.method} ${ctx.request.originalUrl}- ${JSON.stringify(
ctx.request.query.location
)} - Request Successful!!`
);
ctx.body = response.data;
next();
})
.catch(function (error) {
console.error(
`${ctx.request.method} ${ctx.request.originalUrl}- ${JSON.stringify(
ctx.request.query.location
)} - Error fetching data`
);
ctx.throw(404,`Error retrieving data, ${ctx.request.query?.location} ${error.message}`)
});
})
);
if (!module.parent) app.listen(3000);
module.exports.handler = serverless(app);