Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# check-http-status changelog

## 1.3.0 - Oct 15, 2021

### Enhancement

- Allow Sitemap(s) and particular URL(s) together
- Limit Promise to max. 10 concurrent requests
- Show progress of the HTTP Status list

## 1.2.0 - May 21, 2021

### Enhancement
Expand Down
87 changes: 72 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
[![Downloads][downloads-image]][npm-url]
[![Build Status][travis-image]][travis-url]

Easily check status codes, response headers, and redirect chains in `Node.js`
similar as done from the [httpstatus](https://httpstatus.io/) website.

When the site is on VPN so this is where it plays an important role. You can
simply connect your system/machine with VPN and run this package locally so it
can check the status of your VPN connected URL(s).

It can also check the website that are secured with HTTP Authentication.

## Install

Via `npm`
Expand All @@ -18,14 +27,19 @@ Via Yarn
yarn add check-http-status --dev
```

## Usage
## Examples

### Sitemap Example
### Status Code of the Sitemap(s) URL(s)

```node
const checkHttpStatus = require('check-http-status');

checkHttpStatus({
'sitemaps': [
'https://www.trunkcode.com/page-sitemap.xml',
'https://www.trunkcode.com/post-sitemap.xml'
],
'skip200': true, // Do not report the URLs having HTTP code 200.
'export': {
'format': 'xlsx',
'location': '/Users/trunkcode/Desktop/',
Expand All @@ -37,22 +51,25 @@ checkHttpStatus({
},
'headers': {
'Accept': 'text/html',
},
},
'sitemap': [
'https://www.trunkcode.com/page-sitemap.xml',
'https://www.trunkcode.com/post-sitemap.xml'
],
'skip200': true, // Do not report the URLs having HTTP code 200.
}
}
});
```

### URLs Example
### Status Code of the particular URL(s)

```node
const checkHttpStatus = require('check-http-status');

checkHttpStatus({
'urls': [
'http://trunkcode.com/',
'https://example.com/',
'https://example1234.com/',
'https://www.trunkcode.com/',
'https://www.trunkcode.com/test/'
],
'skip200': true, // Do not report the URLs having HTTP code 200.
'export': {
'format': 'xlsx',
'location': '/Users/trunkcode/Desktop/',
Expand All @@ -64,19 +81,59 @@ checkHttpStatus({
},
'headers': {
'Accept': 'text/html',
},
},
'skip200': true, // Do not report the URLs having HTTP code 200.
'urls': [
}
}
});
```

### Status Code of the Sitemap(s) URL(s) with particular URL(s)

```node
const checkHttpStatus = require('check-http-status');

checkHttpStatus({
'sitemaps': [
'https://www.trunkcode.com/page-sitemap.xml',
'https://www.trunkcode.com/post-sitemap.xml'
],
'urls': [
'http://trunkcode.com/',
'https://example.com/',
'https://example1234.com/',
'https://www.trunkcode.com/',
'https://www.trunkcode.com/test/'
]
],
'skip200': true, // Do not report the URLs having HTTP code 200.
'export': {
'format': 'xlsx',
'location': '/Users/trunkcode/Desktop/',
},
'options': {
'auth': {
'password': 'Testing1234',
'username': 'trunkcode'
},
'headers': {
'Accept': 'text/html',
}
}
});
```

## Parameters

| Attributes | Type | Required | Default | Description |
|:----------:|:-------:|:--------:|:-------:|:------------------------------------------------------------------------------------------------:|
| sitemaps | Array | Yes | | Sitemap(s) URL(s) where the Actual site URL(s) needs to be fetched for checking the HTTP Status. |
| urls | Array | Yes | | URL(s) for which HTTP Status needs to be checked. |
| skip200 | Boolean | No | `false` | Whether to list the HTTP status `200` URL(s) or not. |
| export | Object | No | `{}` | Whether to export the status report or not. By default it logs the report on the screen. |
| options | Object | No | `{}` | Define options like HTTP Auth credentials if the site is locked or headers etc. |

**NOTE:** `sitemaps` or `urls` is required. You can define both parameters as
well to fetch URL(s) from sitemap and the URL(s) that are not listed in the
ssitemap, you can provide them separately.

[npm-image]: https://img.shields.io/npm/v/check-http-status.svg
[npm-url]: https://www.npmjs.com/package/check-http-status
[downloads-image]: https://img.shields.io/npm/dt/check-http-status.svg
Expand Down
108 changes: 55 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const consoleColumns = require('./lib/console-columns');
const fetchAllSitemaps = require('./lib/fetch-from-sitemap');
const fetchAllSitemaps = require('./lib/fetch-from-sitemaps');
const fs = require('fs');
const generateExcel = require('./lib/generate-excel');
const httpList = require('./lib/http-list');
Expand All @@ -11,67 +11,69 @@ const httpList = require('./lib/http-list');
* config.
*/
async function checkHttpStatus(config) {
const allowedExportTypes = [
'csv',
'xlsx',
];
var skip200 = false;
var urlsList = [];
const allowedExportTypes = [
'csv',
'xlsx',
];
var skip200 = false;
var urlsList = [];

if (config.skip200) {
skip200 = true;
}
if (config.skip200) {
skip200 = true;
}

if (!config) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Missing required Parameters.');
process.exit();
} else if (!config.options) {
config.options = {};
}
if (!config) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Missing required Parameters.');
process.exit();
} else if (!config.options) {
config.options = {};
}

if (config.sitemap) {
urlsList = await fetchAllSitemaps(config.sitemap);
} else if (config.urls && Array.isArray(config.urls)) {
urlsList = config.urls;
}
if (config.sitemaps) {
urlsList = await fetchAllSitemaps(config.sitemaps);
}

if (urlsList.length === 0) {
console.error('\x1b[31m%s\x1b[0m', 'Error: No URL(s) found.');
process.exit();
} else if (config.export && !config.export.location) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Missing export location.');
process.exit();
} else if (config.export && !fs.existsSync(config.export.location)) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Export Location is undefined.');
process.exit();
}
if (config.urls && Array.isArray(config.urls)) {
urlsList = urlsList.concat(config.urls);
}

const httpStatusList = await httpList(urlsList, config.options, skip200);
if (urlsList.length === 0) {
console.error('\x1b[31m%s\x1b[0m', 'Error: No URL(s) found.');
process.exit();
} else if (config.export && !config.export.location) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Missing export location.');
process.exit();
} else if (config.export && !fs.existsSync(config.export.location)) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Export Location is undefined.');
process.exit();
}

if (config.export && !config.export.format) {
config.export.format = 'xlsx';
}
const httpStatusList = await httpList(urlsList, config.options, skip200);

if (skip200 && httpStatusList.length === 0) {
// Add empty line
console.log();
console.log('\x1b[32m%s\x1b[0m', 'All the URLs are 200 and there is nothing to worry about!');
} else if (config.export && allowedExportTypes.includes(config.export.format)) {
const urlLength = Math.max(...urlsList.map((el) => el.length));
const rowLength = {
'errorMessage': 50,
'requestedUrl': urlLength,
'url': urlLength + 20
};
if (config.export && !config.export.format) {
config.export.format = 'xlsx';
}

generateExcel(httpStatusList, rowLength, config.export);
} else {
consoleColumns(httpStatusList);
}
if (skip200 && httpStatusList.length === 0) {
// Add empty line
console.log();
console.log('\x1b[32m%s\x1b[0m', 'All the URLs are 200 and there is nothing to worry about!');
} else if (config.export && allowedExportTypes.includes(config.export.format)) {
const urlLength = Math.max(...urlsList.map((el) => el.length));
const rowLength = {
'errorMessage': 50,
'requestedUrl': urlLength,
'url': urlLength + 20
};

// Add empty line
console.log();
console.log('\x1b[32m%s\x1b[0m', 'HTTP Status check completed!');
generateExcel(httpStatusList, rowLength, config.export);
} else {
consoleColumns(httpStatusList);
}

// Add empty line
console.log();
console.log('\x1b[32m%s\x1b[0m', 'HTTP Status check completed!');
}

module.exports = checkHttpStatus;
12 changes: 12 additions & 0 deletions lib/check-status-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const axios = require('axios');
/**
* Check HTTP Status as per settings and return status code and other
* information for further process.
*
* @param string urlToCheck URL that needs to be requested.
* @param object axiosOptions Axios Options.
* @param bool redirect Whether the URL is crawling or the redirected URL is crawling.
*
* @returns array Response of checked URL.
*/
function axiosRequest(urlToCheck, axiosOptions, redirect) {
var httpStatus = [
Expand Down Expand Up @@ -81,6 +87,12 @@ function axiosRequest(urlToCheck, axiosOptions, redirect) {
/**
* Call main function to generate array with all the required information
* and await until that all are not completed.
*
* @param string urlToCheck URL that needs to be checked.
* @param object options Axios options.
* @param bool skip200 Whether to log URLs that returns 200 status code or not.
*
* @returns array List of URLs with their status code and other information.
*/
async function checkStatusCode(urlToCheck, options, skip200) {
const statusList = [];
Expand Down
3 changes: 3 additions & 0 deletions lib/console-columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const columnify = require('columnify');

/**
* List down all the URLs with the status code in console.
*
* @param array statusList List of URLs with their status code and other
* information to be logged on console.
*/
function terminalColumns(statusList) {
const data = [];
Expand Down
11 changes: 11 additions & 0 deletions lib/fetch-from-sitemap.js → lib/fetch-from-sitemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const xml2js = require('xml2js');

/**
* Fetch Sitemap URL and dumb all the URLs from the XML to a variable.
*
* @param string sitemapUrl Sitemap URL from which list of URLs needs to be fetched.
*
* @returns array List of URLs fetched from the sitemap.
*/
function fetchFromSitemap(sitemapUrl) {
var errorMessage = 'Error: ' + sitemapUrl + ' returns with status code ';
Expand Down Expand Up @@ -42,6 +46,13 @@ function fetchFromSitemap(sitemapUrl) {
});
}

/**
* Fetch URLs from the sitemap one-by-one.
*
* @param array sitemapUrls List of Sitemap URL(s).
*
* @returns array List of URLs fetched from all the sitemap(s).
*/
async function fetchAllSitemaps(sitemapUrls) {
const sitemapUrlsLists = [];
const urlsList = [];
Expand Down
8 changes: 8 additions & 0 deletions lib/generate-excel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const excelJS = require('exceljs');

/**
* Generate filename with location to save the exported file.
*
* @param object exportDetails Pass export information like format type, location etc.
*
* @returns string File Location with filename.
*/
function exportFileName(exportDetails) {
const currentTimestamp = Math.floor(Date.now() / 1000);
Expand All @@ -16,6 +20,10 @@ function exportFileName(exportDetails) {

/**
* List down all the URLs with the status code in .xlsx format.
*
* @param array statusList List of the URLs needs to bee added in excel file.
* @param object rowLength Length that needs to be set for each column.
* @param object exportDetails Pass export information like format type, location etc.
*/
function generateExcel(statusList, rowLength, exportDetails) {
const location = exportFileName(exportDetails);
Expand Down
Loading