-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (36 loc) · 1.06 KB
/
Copy pathserver.js
File metadata and controls
40 lines (36 loc) · 1.06 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
const fastify = require("fastify");
const func = require("./index").handler;
const normalizeQueryParams = (query) => {
let q = {};
for (const [key, val] of Object.entries(query)) {
q[key] = Array.isArray(val) ? val : [val];
}
return q;
};
const createServer = ({ logger = true } = {}) => {
const server = fastify({ logger });
server.get("/*", async (request, reply) => {
if (request.params["*"] === "favicon.ico") return reply.send("");
const event = {
rawPath: request.params["*"],
path: request.params["*"],
multiValueQueryStringParameters: normalizeQueryParams(request.query),
requestContext: {
http: {
method: "GET",
path: request.params["*"],
},
},
headers: request.headers,
isBase64Encoded: false,
};
const result = await func(event, {}, false);
reply.headers(result.headers).status(result.statusCode).send(result.body);
});
return server;
};
module.exports = createServer;
if (!module.parent) {
const server = createServer();
server.listen(4001);
}