1 | # is-reachable [![Build Status](https://travis-ci.org/sindresorhus/is-reachable.svg?branch=master)](https://travis-ci.org/sindresorhus/is-reachable)
|
2 |
|
3 | > Check if servers are reachable
|
4 |
|
5 | Works in Node.js and the browser *(with a bundler)*.
|
6 |
|
7 | The Node.js version will do a TCP handshake with the target's port. It attempts to detect cases where a router redirects the request to itself.
|
8 |
|
9 | The browser version is limited by the fact that browsers cannot connect to arbitrary ports. It only supports HTTP and HTTPS and the check relies on the `/favicon.ico` path being present.
|
10 |
|
11 |
|
12 | ## Install
|
13 |
|
14 | ```
|
15 | $ npm install is-reachable
|
16 | ```
|
17 |
|
18 |
|
19 | ## Usage
|
20 |
|
21 | ```js
|
22 | const isReachable = require('is-reachable');
|
23 |
|
24 | (async () => {
|
25 | console.log(await isReachable('sindresorhus.com'));
|
26 | //=> true
|
27 |
|
28 | console.log(await isReachable('google.com:443'));
|
29 | //=> true
|
30 | })();
|
31 | ```
|
32 |
|
33 |
|
34 | ## API
|
35 |
|
36 | ### isReachable(targets, options?)
|
37 |
|
38 | Returns a `Promise<boolean>` which is `true` if any of the `targets` are reachable.
|
39 |
|
40 | #### targets
|
41 |
|
42 | Type: `string | string[]`
|
43 |
|
44 | One or more targets to check. Can either be `hostname:port`, a URL like `https://hostname:port` or even just `hostname`. `port` must be specified if protocol is not `http:` or `https:` and defaults to `443`. Protocols other than `http:` and `https:` are not supported.
|
45 |
|
46 | #### options
|
47 |
|
48 | Type: `object`
|
49 |
|
50 | ##### timeout
|
51 |
|
52 | Type: `number`<br>
|
53 | Default: `5000`
|
54 |
|
55 | Timeout in milliseconds after which a request is considered failed.
|
56 |
|
57 |
|
58 | ## Related
|
59 |
|
60 | - [is-online](https://github.com/sindresorhus/is-online) - Check if the internet connection is up
|
61 |
|
62 |
|
63 | ## Maintainers
|
64 |
|
65 | - [Sindre Sorhus](https://github.com/sindresorhus)
|
66 | - [silverwind](https://github.com/silverwind)
|