-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (52 loc) · 1.6 KB
/
server.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 express = require('express');
const morgan = require('morgan');
const fs = require('fs');
const path = require('path');
const { isValidUrl, scan, response, sendResponse } = require('./utils');
const { PORT } = process.env;
const server = express();
const port = PORT || 4117;
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' });
server.use(morgan('combined', { stream: accessLogStream }));
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
server.use('/captures', express.static('captures'));
const delayedResponse = async (url, requestBody) => {
setTimeout( async () => {
const results = await scan(url);
const responseBody = response(url, results);
sendResponse(requestBody, responseBody);
}, 0);
};
server.get('/', (req, res, next) => {
res.json({ msg: 'The Slack-A11y API seems to be operating.' });
});
server.post('/', async (req, res, next) => {
let pageUrl = req.body.text;
if ( !pageUrl.startsWith('http') ) {
pageUrl = 'http://' + pageUrl;
}
if ( isValidUrl(pageUrl) ) {
res.json({
"response_type": "ephemeral",
"text": `Analyzing ${pageUrl}. Please wait!`
});
delayedResponse(pageUrl, req.body);
} else {
res.json({
"response_type": "ephemeral",
"text": `${pageUrl} doesn't seem to be a valid url. Try something else!`
});
}
});
server.post('/direct', async (req, res, next) => {
const pageUrl = req.body.url;
const results = await scan(pageUrl);
res.json({
"URL": pageUrl,
"results": results,
});
});
server.listen(port, () => {
console.log(`Server started at localhost:${port}`)
});