What is prepend-http?
The prepend-http npm package is a utility that ensures a URL has a protocol (http or https) prepended to it. This is useful for normalizing URLs and ensuring they are valid for HTTP requests.
What are prepend-http's main functionalities?
Prepend HTTP to URL
This feature allows you to prepend 'http://' to a URL if it does not already have a protocol. This is useful for ensuring that URLs are valid for HTTP requests.
const prependHttp = require('prepend-http');
const url = 'example.com';
const fullUrl = prependHttp(url);
console.log(fullUrl); // 'http://example.com'
Prepend HTTPS to URL
This feature allows you to prepend 'https://' to a URL if it does not already have a protocol. This is useful for ensuring that URLs are secure.
const prependHttp = require('prepend-http');
const url = 'example.com';
const fullUrl = prependHttp(url, {https: true});
console.log(fullUrl); // 'https://example.com'
Other packages similar to prepend-http
normalize-url
The normalize-url package is a more comprehensive solution for URL normalization. It not only ensures that a URL has a protocol, but also handles other aspects of URL normalization such as removing duplicate slashes, sorting query parameters, and more. It is more feature-rich compared to prepend-http.
url-parse
The url-parse package is a robust URL parsing library that can handle a wide range of URL manipulations, including ensuring that a URL has a protocol. It provides more extensive parsing and manipulation capabilities compared to prepend-http.
prepend-http
Prepend https://
to humanized URLs like sindresorhus.com
and localhost
Install
$ npm install prepend-http
Usage
const prependHttp = require('prepend-http');
prependHttp('sindresorhus.com');
prependHttp('localhost', {https: false});
prependHttp('https://sindresorhus.com');
API
prependHttp(url, [options])
url
Type: string
URL to prepend https://
to.
options
Type: object
https
Type: boolean
Default: true
Prepend https://
instead of http://
.
License
MIT © Sindre Sorhus