-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (58 loc) · 1.88 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
const path = require("path");
const express = require("express");
const app = express();
const util = require("util");
const whois = util.promisify(require("whois").lookup);
var bodyParser = require("body-parser");
const log = require("morgan");
const ROOT = path.join(__dirname, "public");
const VIEW_ROOT = path.join(__dirname, "pages");
require("dotenv").config();
const PORT = process.env.PORT || 8080;
app.use(bodyParser());
app.set("json spaces", 2);
app.use(log("dev"));
app.use(express.static(ROOT));
app.set("view engine", "ejs");
app.set("views", VIEW_ROOT);
app.use((req, res, next) => {
res.locals.hostname = req.hostname;
next();
});
app.get("/", async (req, res) => {
res.render("index");
});
app.get(["/whois/:query", "/whois"], async (req, res, next) => {
let query = req.params.query || req.query.query;
if (!query) return res.render("result", { query, result: "Error: Bad Request" });
try {
// if (cache[query]) return res.render("result", { query, result: cache[query] });
let result = (await whois(query, { follow: 0 })).trim();
// cache[query] = result;
res.render("result", { query, result });
} catch (e) {
res.render("result", { query, result: "Error : " + e.message });
console.log(e);
}
});
app.get(['/api/whois/:query', '/api/whois'], async (req, res, next) => {
res.type("text/plain");
let query = req.params.query || req.query.query;
if (!query) return res.status(400).send('400: Bad Request');
try {
// if (cache[query]) return res.send(cache[query].trim());
let result = await whois(query, { follow: 0 });
// cache[query] = result;
res.send(result.trim());
} catch (e) {
res.status(500).send("Error: "+e.message);
console.log(e);
}
})
app.use((req, res, next) => {
res.sendStatus(404);
});
const listener = app.listen(PORT);
console.log(
"[ EXPRESS ] Your app listen on port " + listener.address()?.port || 443
);