-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-broken-links.js
40 lines (34 loc) · 1.14 KB
/
check-broken-links.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
const blc = require('broken-link-checker');
const express = require('express');
const fs = require('fs');
const app = express()
app.use(express.static('build/site'))
const server = app.listen(3000, () => {
console.log(`Started serving files on port 3000!`)
})
String.prototype.trimUrl = function () {
return this.replaceAll("http://localhost:3000", "")
}
const brokenLinks = []
const siteChecker = new blc.SiteChecker({
excludeExternalLinks: true,
honorRobotExclusions: false,
maxSocketsPerHost: 3,
maxSockets: 10,
}, {
link: (result) => {
if (result.broken && result.url.resolved) {
let statusCode = result.http.response.statusCode;
console.log(`Broken link: ${result.url.resolved?.trimUrl()} (${statusCode}) on page ${result.base.resolved?.trimUrl()}`)
brokenLinks.push(result.url.resolved.trimUrl())
}
},
end: () => {
server.close()
if (brokenLinks.length > 0) {
process.exitCode = 1
}
}
}
)
siteChecker.enqueue("http://localhost:3000/home/index.html")