Socket
Book a DemoInstallSign in
Socket

@polka/redirect

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polka/redirect

A response helper for URL redirects

latest
Source
npmnpm
Version
1.0.0-next.0
Version published
Weekly downloads
122
-1.61%
Maintainers
1
Weekly downloads
 
Created
Source

@polka/redirect npm

A response helper for URL redirects; not limited to Polka!

Allows you to easily formulate HTTP redirect responses.

It will set the Location header with the target value, which may be fully-qualified URLs or may be relative to the current URL of the request.

Install

$ npm install --save @polka/redirect

Usage

const polka = require('polka');
const redirect = require('@polka/redirect');

polka()
  // All examples start from "/foo/bar" path:
  .get('/foo/bar', (req, res) => {
    // relative paths
    redirect(res, '../'); //=> (302) "/"
    redirect(res, './'); //=> (302) "/foo/"
    redirect(res, 'baz'); //=> (302) "/foo/baz"

    // absolute paths
    redirect(res, '/'); //=> (302) "/"
    redirect(res, '/bar'); //=> (302) "/bar"

    // external paths
    redirect(res, 'https://example.com'); //=> (302) "https://example.com/"
    redirect(res, 'https://example.com/foo'); //=> (302) "https://example.com/foo"

    // custom statusCode
    redirect(res, 301, '/'); //=> (301) "/"
    redirect(res, 301, '/baz'); //=> (301) "/baz"
    redirect(res, 301, 'baz?name=bat'); //=> (301) "/foo/baz?name=bat"
    redirect(res, 301, 'https://example.com/foo'); //=> (301) "https://example.com/foo"

    // "back" w/ "Referrer" HTTP header
    redirect(res, 'back'); //=> (302) "/previous/path"
    redirect(res, 301, 'back'); //=> (301) "/previous/path"

    // "back" without "Referrer" HTTP header
    redirect(res, 'back'); //=> (302) "/"
    redirect(res, 301, 'back'); //=> (301) "/"
  });

API

redirect(res, location)

redirect(res, code, location)

res

Type: ServerReponse

The outgoing HTTP response.

code

Type: Number
Default: 302

The statusCode for your response.

location

Type: String
Default: ''

The Location target for your redirect. It may be declared as:

  • an absolute path – eg /, /foo/bar
  • a relative path – eg ., .., ./, ../, foo/bar
  • a complete URL – eg https://example.com/foo/bar
  • or "back", which redirects to "/" only when the Referrer is unavailable

Support

Any issues or questions can be sent to the Polka repository.
However, please specify that your inquiry is about @polka/url specifically.

License

MIT © Luke Edwards

FAQs

Package last updated on 10 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts