-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
114 lines (95 loc) · 2.62 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const express = require("express");
const https = require("https");
require("dotenv").config();
const app = express();
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
function httpRequest(method, url, body = null) {
if (!["get", "post", "head"].includes(method)) {
throw new Error(`Invalid method: ${method}`);
}
let urlObject;
try {
urlObject = new URL(url);
} catch (error) {
throw new Error(`Invalid url ${url}`);
}
if (body && method !== "post") {
throw new Error(
`Invalid use of the body parameter while using the ${method.toUpperCase()} method.`
);
}
let options = {
method: method.toUpperCase(),
hostname: urlObject.hostname,
port: urlObject.port,
path: urlObject.pathname,
headers: {
Authorization: process.env.AUTH_KEY,
"accept-version": "1.0.0",
},
};
if (body) {
options.headers = { "Content-Length": Buffer.byteLength(body) };
}
return new Promise((resolve, reject) => {
const clientRequest = https.request(options, (incomingMessage) => {
// Response object.
let response = {
statusCode: incomingMessage.statusCode,
headers: incomingMessage.headers,
body: [],
};
// Collect response body data.
incomingMessage.on("data", (chunk) => {
response.body.push(chunk);
});
// Resolve on end.
incomingMessage.on("end", () => {
if (response.body.length) {
response.body = response.body.join();
try {
response.body = JSON.parse(response.body);
} catch (error) {
// Silently fail if response is not JSON.
}
}
resolve(response);
});
});
// Reject on request error.
clientRequest.on("error", (error) => {
reject(error);
});
// Write request body if present.
if (body) {
clientRequest.write(body);
}
// Close HTTP connection.
clientRequest.end();
});
}
app.listen(process.env.PORT || 3000, () => console.log("Webhook is listening"));
app.get("/", (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
); // If needed
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,content-type"
); // If needed
res.setHeader("Access-Control-Allow-Credentials", true); // If needed
httpRequest(
"get",
"https://api.webflow.com/collections/6051cb041c2ff4cd91f14729/items"
).then(function (result) {
console.log(result);
res.status(200).json(result);
});
});