-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
38 lines (31 loc) · 909 Bytes
/
index.ts
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
import express from 'express';
import cors from 'cors';
import isPorn from 'is-porn';
const app = express();
const port = process.env.PORT ?? 3001;
app.use(cors());
/* test preview */
const isPornPromise = (url) => new Promise((resolve, reject) => {
isPorn(url, (error, status) => {
if (error) reject(error);
else resolve(status);
});
});
app.get('/', async (req, res, next) => {
const { site } = req.query;
let url = '';
try {
url = new URL(site).hostname;
} catch (error) {
return res.status(400).json({ error: 'Invalid url: example.com?site=https://delfi.lt' });
}
try {
const status = await isPornPromise(url);
return res.json({ url, status });
} catch (error) {
return res.status(500).json({ error: error.message });
}
});
app.listen(port, () => {
console.log(`Ready http://localhost:${port}`);
});