Creating Custom Routes

If you'd like more flexibility in URL paths, such as adding folders within folders, or would like to use request paths as variables, you can create your own route configuration.

For example:


These examples show an extra subfolder, and with the third request we're passing a book ID in the request path (rather than as a URL parameter).


Setting Your Custom Routes

Custom routes are handled by editing the .blinkmrc.json file in your project folder.

Example .blinkmrc.json file:

{
  "server": {
    "project": "company-project.api.blinkm.io",
    "region": "ap-southeast-2",
    "cors": true,
    "routes": [
      {
        "route": "/api/request",
        "module": "./api/request/index.js"
      },
      {
        "route": "/api/books",
        "module": "./api/books.js"
      },
      {
        "route": "/api/books/{id}",
        "module": "./api/book.js"
      }
    ]
  }
}


"routes" is an array of custom route objects. A route object is made up of two values:

route:

The route value is the URL path you will use to make requests to this API.

module:

The module value is the path to the script that will handle this route.


Multi-Folder Example:
 

      {
        "route": "/api/request",
        "module": "./api/request/index.js"
      }


In the example above, notice we're using a multiple folder request.

This means any requests to:

https://company-project-dev.api.blinkm.io/api/request/

...would point to the ./api/request/index.js file in your project folder. This gives you the potential to group similar endpoints together in their own folder.


Custom File Name
 

      {
        "route": "/api/books",
        "module": "./api/books.js"
      }


In the example above, notice we are not using the default index.js file, as is required when using the automatic routing as shown first on this page.

This means any requests to:

https://company-project-dev.api.blinkm.io/api/books/

...would point to the ./api/books.js file in your project folder.


Request Parameter in URL Path
 

      {
        "route": "/api/books/{id}",
        "module": "./api/book.js"
      }


This example shows us the ability to pass a value to our script from the URL request path.

This means any requests to:

https://company-project-dev.api.blinkm.io/api/books/123

...would pass the value "123" to your script ./api/book.js file in your project folder, as the value of "id":


This value can then be accessed from the request object:

module.exports.get = function (request) {
  return 'ID Parameter: ' + request.url.params.id
}


Examples

If you haven't yet created any routes for your project, you can quickly get started by downloading the examples ZIP from the Server CLI Examples and Tips page.


Next Step

Configuring your Environment Variables