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.
- Install the module with:
npm install redirect-rest - Go to your Javascript app folder
- Create a
.redirect-rest.jsonfile (read more) - Launch the server with
redirectrest - Open
http://localhost:4242/and enjoy your redirects!
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 port -
livereload: Make use of LiveReload to refresh the browser on local changes -
livereloadPort: 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/usersredirects to:http://example.com/api/users -
http://localhost:4242/example/rolesredirects to:http://example.com/api/roles -
http://localhost:4242/exmaple/whateverredirects to:http://example.com/api/whatever -
http://localhost:4242/anotherexample/clientsredirects to:http://anotherexample.com/api/clients -
http://localhost:4242/anoherexample/productsredirects to:http://anotherexample.com/api/products -
http://localhost:4242/anotherexample/whateverredirects to:http://anotherexample.com/api/whatever
ASP.NET, PHP and JSP files will be treated as text/html.
- Add
port,proxy,livereloadandlivereloadPortconfig options (v2.1.0, thanks to @howardh) - Remove
routes. Now all requests are redirected toremote_url(v2.0.0) - Remove support for redirections to different remote paths (v1.0.0)
- Support querystrings (v0.0.9)
- Redirect local requests to different remote path (v0.0.8)
- Implement error forwarding to local server from remote server (v0.0.7)
- Better logging system (v0.0.5)
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 \_(ʘ_ʘ)_/
Copyright (c) 2014 Juan G. Hurtado Licensed under the MIT license.