forked from JustinBeckwith/linkinator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
82 lines (68 loc) · 2.12 KB
/
Copy pathapi.js
File metadata and controls
82 lines (68 loc) · 2.12 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import link from 'linkinator';
/**
* This is a simple example of using the API to do a basic scan. You can
* include a bunch of other parameters to customize the behavior.
*/
async function simple() {
const results = await link.check({
path: 'http://example.com',
});
// To see if all the links passed, you can check `passed`
console.log(`Passed: ${results.passed}`);
// Show the list of scanned links and their results
console.log(results);
// Example output:
// {
// passed: true,
// links: [
// {
// url: 'http://example.com',
// status: 200,
// state: 'OK'
// },
// {
// url: 'http://www.iana.org/domains/example',
// status: 200,
// state: 'OK'
// }
// ]
// }
}
/**
* This more complex example shows how
*/
async function complex() {
// Create a new `LinkChecker` that we'll use to run the scan.
const checker = new link.LinkChecker();
// Respond to the beginning of a new page being scanned
checker.on('pagestart', (url) => {
console.log(`Scanning ${url}`);
});
// After a page is scanned, check out the results!
checker.on('link', (result) => {
// Check the specific url that was scanned
console.log(` ${result.url}`);
// How did the scan go? Potential states are `BROKEN`, `OK`, and `SKIPPED`
console.log(` ${result.state}`);
// What was the status code of the response?
console.log(` ${result.status}`);
});
// Go ahead and start the scan! As events occur, we will see them above.
const result = await checker.check({
path: 'http://example.com',
// Port: 8673,
// recurse?: true,
// linksToSkip: [
// 'https://jbeckwith.com/some/link',
// 'http://example.com'
// ]
});
// Check to see if the scan passed!
console.log(result.passed ? 'PASSED :D' : 'FAILED :(');
// How many links did we scan?
console.log(`Scanned total of ${result.links.length} links!`);
// The final result will contain the list of checked links, and the pass/fail
const brokeLinksCount = result.links.filter((x) => x.state === 'BROKEN');
console.log(`Detected ${brokeLinksCount.length} broken links.`);
}
await simple(); // Or, complex();