forked from jacktuck/unfurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
44 lines (39 loc) · 1.11 KB
/
example.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
const http = require("http");
const { parse } = require("url");
const { unfurl } = require("./dist");
http
.createServer(function (req, res) {
const isUrl = /https?:\/\//;
const { url } = parse(req.url, true).query;
if (!url) {
res.writeHead(400, "Please submit a url with querystring");
res.end();
return;
}
if (!isUrl.test(url)) {
res.writeHead(400, "Please only submit http(s) urls");
res.end();
return;
}
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, PUT, POST, DELETE, OPTIONS"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
);
unfurl(url)
.then((data) => {
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.end(JSON.stringify(data));
})
.catch((err) => {
console.error(err);
res.writeHead(500, err);
res.end();
});
})
.listen(process.env.PORT || 3000); //eslint-disable-line