redirect-rest
v2.2.0
Published
Redirect REST requests to another server
Downloads
11
Readme
redirect-rest
Redirect REST requests to other servers, so you can develop your Javascript app without having to launch the backend server on your machine or worry about CORS.
Getting Started
- Install the module with:
npm install redirect-rest
- Go to your Javascript app folder
- Create a
.redirect-rest.json
file (read more) - Launch the server with
redirectrest
- Open
http://localhost:4242/
and enjoy your redirects!
Config options
To configure the redirections and everything else for redirectrest
, create a .redirect-rest.json
file on the root folder of your application.
Inside this JSON file you can have this options:
public_path
: Path of the assets that will be published on the HTTP server launched byredirectrest
. Defaults to:./
html_extensions
: Array of file extensions which will be treated astext/html
(just in case you are required to have an HTML file with backend extension: PHP, JSP…). Defaults to:["jsp", "php", "html"]
redirection_rules
: array that contains the redirection rules to redirect to one or many remote servers. Required. Each item in the array is formed by: -path
: path used in the request in order to redirect to the remote server. Required. -remote_url
: Base URL of the remote server, where the local requests will be redirected. Required. -proxy
: Proxy to be used to make remote server requests. Optional.port
: Local server portlivereload
: Make use of LiveReload to refresh the browser on local changeslivereloadPort
: Port to be used with LiveReload
For example: Imagine you have a JS app and two REST API services on http://example.com/api/
and http://anotherexample.com/api/
. Your .redirect-rest.json
file would be:
{
"html_extensions" : ["aspx", "php", "jsp"],
"public_path" : "./",
"redirection_rules" : [
{
"path": "/example",
"remote_url": "http://example.com/api"
},
{
"path": "/anotherExample",
"remote_url": "http://anotherExample.com/api"
}
]
}
This way you'll have your JS app published under http://localhost:4242/
, and requests will be redirected as follows:
http://localhost:4242/example/users
redirects to:http://example.com/api/users
http://localhost:4242/example/roles
redirects to:http://example.com/api/roles
http://localhost:4242/exmaple/whatever
redirects to:http://example.com/api/whatever
http://localhost:4242/anotherexample/clients
redirects to:http://anotherexample.com/api/clients
http://localhost:4242/anoherexample/products
redirects to:http://anotherexample.com/api/products
http://localhost:4242/anotherexample/whatever
redirects to:http://anotherexample.com/api/whatever
ASP.NET, PHP and JSP files will be treated as text/html
.
TO-DO
- [x] Add
port
,proxy
,livereload
andlivereloadPort
config options (v2.1.0, thanks to @howardh) - [x] Remove
routes
. Now all requests are redirected toremote_url
(v2.0.0) - [x] Remove support for redirections to different remote paths (v1.0.0)
- [x] Support querystrings (v0.0.9)
- [x] Redirect local requests to different remote path (v0.0.8)
- [x] Implement error forwarding to local server from remote server (v0.0.7)
- [x] Better logging system (v0.0.5)
Acknowledgments
The original idea behind redirect-rest was by @drmillan. He was planning on doing something much more simple, and written in some archaic and obscure language such as PHP.
I couldn't allow that \_(ʘ_ʘ)_/
License
Copyright (c) 2014 Juan G. Hurtado Licensed under the MIT license.