-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
33 lines (28 loc) · 811 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
import express from 'express';
import isPorn from 'is-porn';
const app = express();
const port = process.env.PORT ?? 3001;
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' });
}
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}`);
});