0.0.2 • Published 6 years ago
@centralping/http-redirect v0.0.2
@CentralPing/http-redirect
An HTTP request handler for node based http.Server
style servers. It automatically sets the correct status code and headers for a redirect response. Function invoctioners will still need to end the request manually.
Notes
- The redirect URL is required.
- No safeguards against looped redirects.
Installation
npm i --save @centralping/http-redirect
API Reference
httpRedirect~redirect ⇒ function
Configures a request handler for http.Server
style servers.
Kind: inner property of httpRedirect
Returns: function - - http.Server
style request handler
Param | Type | Default | Description |
---|---|---|---|
uri | String | the URI/URL to redirect HTTP request to. | |
options | Object | optional configuration for HTTP response. | |
options.statusCode | Number | 301 | the response HTTP status code. |
options.header | Object | any additional valid HTTP response header values. | |
options.header.cacheControl | String | 'public, immutable, s-maxage=31536000' | the HTTP response header cache control. |
Examples
Simple
const redirect = require('@centralping/http-redirect');
const redirector = redirect('https://foo.bar');
const server = someServerCreator();
// Redirect all the things
server.route('*', (req, res) => {
// Do something interesting perhaps?
// ...
// Redirect this request
redirector(req, res);
// Important!!
// End the response to send it on its merry way
res.end();
});
Middleware
const redirect = require('@centralping/http-redirect');
const redirector = redirect('https://foo.bar');
const server = someServerCreator();
// Redirect all the things
server.route('*', redirector, (req, res) => res.end());
Test
npm test
With coverage reporting:
npm test -- --coverage
With file watch:
npm run watch
License
MIT
0.0.2
6 years ago