forked from b0o/surfingkeys-conf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (60 loc) · 1.7 KB
/
app.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
const express = require("express")
const morgan = require("morgan")
const fetch = require("node-fetch")
const app = express()
app.use(morgan("dev"))
app.get("/domainr", async (req, res) => {
if (!(req.query.q && typeof req.query.q === "string")) {
res.status(400).send({ error: "Invalid request" })
return
}
const q = encodeURIComponent(req.query.q)
const key = `mashape-key=${process.env.KEY_DOMAINR}`
const baseURL = "https://domainr.p.mashape.com/v2/"
const domains = {}
try {
const subRes = await fetch(`${baseURL}search/?${key}&query=${q}`)
const data = await subRes.json()
if (!Array.isArray(data.results)) {
res.status(500).send({ error: "Error querying Domainr" })
return
}
Object.assign(
domains,
data.results.reduce((acc, d) => {
acc[d.domain] = { status: ["unknown"], summary: "unknown" }
return acc
}, {})
)
} catch (e) {
res.status(500).send({ error: "domainr search error" })
return
}
const domainsStr = Object.keys(domains).join(",")
if (domainsStr.length === 0) {
res.send(domains)
return
}
try {
const subRes = await fetch(`${baseURL}status/?${key}&domain=${domainsStr}`)
const data = await subRes.json()
if (!Array.isArray(data.status)) {
res
.status(500)
.send({ error: "domainr status: unexpected empty response" })
return
}
Object.assign(
domains,
data.status.reduce((acc, d) => {
acc[d.domain] = { status: d.status.split(" "), summary: d.summary }
return acc
}, {})
)
} catch (e) {
res.status(500).send({ error: "domainr status error" })
return
}
res.send(domains)
})
module.exports = app