1 | # Check Broken Links
|
2 |
|
3 | [![Build Status](https://travis-ci.org/actuallymentor/check-broken-links.svg?branch=master)](https://travis-ci.org/actuallymentor/check-broken-links)
|
4 |
|
5 | An npm package that checks an array of supplied links for broken links.
|
6 |
|
7 | ```js
|
8 | const check = require( 'check-broken-links' )
|
9 | const containsBroken = [
|
10 | 'https://www.iCONTAINbrokenlinks.com',
|
11 | 'https://www.iAMbroken.com',
|
12 | 'https://www.iamfine.com'
|
13 | ]
|
14 | check( 'https://base.url/', containsBroken ).then( brokenlinks => {
|
15 | console.log( brokenlinks )
|
16 | /*
|
17 | { top: [ { url: 'https://www.iAMbroken.com', err: [Object] } ],
|
18 | crawled:
|
19 | [ { link: 'https://iwasinside.com/iCONTAINbrokenlinks ',
|
20 | sources: [ 'https://iwasfoundinthislinkyou supplied.com', 'https://butalsointhisone.com' ] }
|
21 | ] },
|
22 | allchecked: { [ link: '', sources: [] ] } // This would obviously be populated
|
23 | */
|
24 | } )
|
25 | ```
|
26 |
|
27 | The base url for the relative links has a required trailing slash.
|
28 |
|
29 | ## Mocha testing example
|
30 |
|
31 | Let's say you want to check if your project has any broken links:
|
32 |
|
33 | ```js
|
34 | // Get polyfill so we can use full ES6 in the tests
|
35 | import 'babel-polyfill'
|
36 |
|
37 | // Get the expect functionality
|
38 | import { expect } from 'chai'
|
39 |
|
40 | // Display results as table
|
41 | import 'console.table'
|
42 |
|
43 | // Import the link checker
|
44 | const check = require( 'check-broken-links' )
|
45 |
|
46 | // We do not use arrow syntax here because that would break the this.timeout
|
47 | describe( 'Links in the project', function( ) {
|
48 |
|
49 | // Set the timeouts high so that all links can be checked without many or slow requests crashing the test
|
50 | this.timeout( process.env.maxtimeout || ( 1000 * 60 * 5 ) )
|
51 |
|
52 | // Of course you would do some dynamic stuff to find links in the test below
|
53 | let yourlinks = []
|
54 |
|
55 | // The current setup uses mocha in a promise fashion
|
56 | // You could also have the callback be done => {}, but then need to call done() after the expect()
|
57 | it( 'All return 200', () => {
|
58 | return check( yourlinks )
|
59 | .then( brokenlinkarray => {
|
60 | if ( broken.top.length > 0 ) console.log( 'Broken Top levels' ); console.table( broken.top )
|
61 | if ( broken.crawled.length > 0 ) console.log( 'Broken crawled links' ); console.table( broken.crawled )
|
62 | expect( broken.crawled.length ).to.equal( 0 )
|
63 | expect( broken.top.length ).to.equal( 0 )
|
64 | } )
|
65 | } )
|
66 | } )
|
67 | ```
|
68 |
|
69 | ## Configuration
|
70 |
|
71 | Want the module to console.log every request set the environment variable 'verbose' to true:
|
72 |
|
73 | ```shell
|
74 | verbose=true node app.js
|
75 | ```
|
76 |
|
77 | This will log out every request. Successfull requests turn green, failed ones red.
|
78 |
|
79 | The GET requests have a default timeout of 60 seconds. You can set this using the 'gettimeout' environment variable in seconds.
|
80 |
|
81 | ```shell
|
82 | # 2 minute timeout
|
83 | gettimeout=120 node app.js
|
84 | ```
|