View on GitHub

Api Redirect

A beautiful api redirect for all web projects :)

Download this project as a .zip file Download this project as a tar.gz file

APIRedirect

A beautiful api redirect for all web projects.

A improvement from NodeApiRedirect.

Summary

Configuration

Before, see the configuration.sample.json file located at configuration folder.

Files

API Redirect always read configuration.json file located at configuration folder.

Multiple files

You can have several others configuration files, but remember to set default property in configuration.json, like this:

configuration.google.json

{
  "host": "https://google.com",
  "port": 3000,
  "root_folder": "/path/to/project",
  "bind": [
    {
      "uri": "/index",
      "path": "/index.html"
    },
    {
      "uri": "/resources/",
      "path": "/resources/"
    }
  ]
}

configuration.json

{
  "default": "google"
}

In this case, default property is a alias to your API (configuration. + api + .json).

Only one file

You can also have several APIs in only one file, like this:

configuration.google.json

{
  "default": "api_1",
  "api_1": {
    "host": "https://api_1.com",
    "port": 3000,
    "root_folder": "/path/to/project",
    "bind": [
      {
        "uri": "/index",
        "path": "/index.html"
      },
      {
        "uri": "/resources/",
        "path": "/resources/"
      }
    ]
  },
  "api_2": {
    "host": "https://api_2.test.com",
    "port": 4000,
    "root_folder": "/path/to/project",
    "bind": [
      {
        "uri": "/index",
        "path": "/index.html"
      },
      {
        "uri": "/resources/",
        "path": "/resources/"
      }
    ]
  }
}

In this case, default property is a key to your API object (configurationFileContent[api]).

Properties

required

A alias to your API.

required

Your API host.

required

Server listening at this property.

required

Root folder to your project.

required

Object to bind a uri in a path or folder.

He is used in express configuration like this:

app.use(uri, express.static(root_folder + path));

or

app.use(express.static(folder));

Attention: If you using for client side development, needed a url to your index.html file, because all requests at root path are redirected to your API.

For instance:

{
    "uri": "/index",
    "path": "/index.html"
}

Now, just open your API host with /index.

Example: http://localhost:3000/index

optional

This object is used to set properties for all requests.

For instance:

"request": {
    "timeout": 60000,
    "headers": {
        "Authorization": "Basic VGhhbmsgeW91IGZvciBkZWNvZGUgdGhpcyBzdHJpbmcuIDop"
    }
}

optional

This object is used to override responses. Very good to tests.

For instance:

"override_responses": {
    "/login": [
        {
            "field": "targetUrl",
            "value": "http://localhost:3000/index"
        }
    ]
}

In this case, the field targetUrl of response in request /login will be change to http://localhost:3000/index.

value can to accept chain objects like 'myObject.myProperty.test'.

optional

This object is used to set log properties.

For instance:

"log": {
    "colors": true,
    "show_response": true,
    "show_request_info": true,
    "show_request_error": true
}

How to run

First, type npm install to install dependencies.

In root folder of this project, type:

node main.js

But you can pass some arguments also:

For instance:

node main.js --host http://anotherdomain.test.com --port 3000

Issues

Questions, bug reports, improve documentation, and feature request please search through existing issue or open a one new issue. If you need support send me an email.

Thanks

Augusto Monteiro for multiple configuration files and CLI options.

License

API Redirect is released under the MIT license. See LICENSE for more details.

More

API Redirect is a work in progress, feel free to improve it.