node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

html-redirect

npm

html-redirect

Build Status Dependency Status

Generate HTML redirection page.

Example

htmlRedirect('http://example.com')
  .pipe(fs.createWriteStream('example1.html'));

example1.html:

<!DOCTYPE html>
<html>
  <head>
    <title>(redirect)</title>
    <meta http-equiv="refresh" content="1;url=http://example.com/">
    <script>window.location.replace("http://example.com/");</script> 
  </head>
  <body><a href="http://example.com/">Click me</a></body>
</html>

API

htmlRedirect(url, [options])

Returns a readable stream of HTML.

Option Type Required? Default
timeout number No 1
title string No
placeholder string No
replaceBody boolean No false

options.timeout is a timeout for meta-tag redirection. JS redirection will fire instantaneously regardless of the value of this option.

options.title is the value of <title>.

options.placeholder is either the text under default <a> or the whole <body> in HTML depending on options.replaceBody.

htmlRedirect.createStream(url, [options])

Returns a transform stream. Body content → HTML page.

Option Type Required? Default
timeout number No 1
title string No

CLI

Usage:  html-redirect [OPTION]... URL
 
Options:
  --title           Page title
  --placeholder     Text under <a> in the page body, or the page body itself
  --replace-body    If true, --placeholder replaces the whole body of a page
  --timeout         http-equiv refresh timeout

More examples

Set some options

htmlRedirect('http://example.com', {
  timeout: 0,
  title: 'please wait...',
  placeholder: 'Your browser does not support redirection. Please click this link.'
}).pipe(fs.createWriteStream('example2.html'));

example2.html:

<!DOCTYPE html>
<html>
  <head>
    <title>please wait...</title>
    <meta http-equiv="refresh" content="0;url=http://example.com/">
    <script>window.location.replace("http://example.com/");</script> 
  </head>
  <body><a href="http://example.com/">Your browser does not support redirection. Please click this link.</a></body>
</html>

Replace body

htmlRedirect('http://example.com', {
  timeout: 3,
  title: 'please wait...',
  placeholder: 'Your browser does not support redirection. Please click <a href="http://example.com">this link</a>.',
  replaceBody: true
}).pipe(fs.createWriteStream('example3.html'));

example3.html:

<!DOCTYPE html>
<html>
  <head>
    <title>please wait...</title>
    <meta http-equiv="refresh" content="3;url=http://example.com/">
    <script>window.location.replace("http://example.com/");</script> 
  </head>
  <body>Your browser does not support redirection. Please click <a href="http://example.com">this link</a>.</body>
</html>

Use it as a transform stream

example4.js:

process.stdin
  .pipe(htmlRedirect.createStream('http://example.com/'))
  .pipe(process.stdout);

Try it in the console:

printf 'Contents from the <b>stdin</b>.' |node example.js
<!DOCTYPE html>
<html>
  <head>
    <title>(redirect)</title>
    <meta http-equiv="refresh" content="1;url=http://example.com/">
    <script>window.location.replace("http://example.com/");</script> 
  </head>
  <body>Contents from the <b>stdin</b>.</body>
</html>

Install

npm install html-redirect

License

MIT