-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (51 loc) · 1.42 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
import dotenv from 'dotenv';
import express from 'express';
import pa11y from 'pa11y';
import cors from 'cors';
import morgan from 'morgan';
import chalk from 'chalk';
import puppeteer from 'puppeteer';
dotenv.config();
const app = express();
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use(cors());
app.get('/api/test', async (req, res) => {
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
const page = await browser.newPage();
try {
if(!req.query.url) {
return res.status(400).json({error: 'URL is required'});
// return res.status(400).send('URL is required');
}
const results = await pa11y(req.query.url, {
runners: [
'axe',
'htmlcs'
],
browser: browser,
page,
});
res.status(200).json(results);
// Close the browser instance and pages now we're done with it
await page.close();
await browser.close();
} catch (error) {
// Close the browser instance and pages if theys exist
if (page) {
await page.close();
}
if (browser) {
await browser.close();
}
return res.status(500).json({error: error.message});
// res.status(500).send('Server Error');
}
});
// Set static folder
app.use(express.static('public'));
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(chalk.yellow(`Server is running in ${process.env.NODE_ENV} on port ${PORT}!`))
});