On ActionHero Routing

actionhero javascript node.js 
↞ See all posts

I’ve been seeing questions about how actionHero’s routes interact with the actions. Here’s a bit more of an explanation (which I’ll keep to date on the ActionHero documentation wiki):

This variables in play here are:

1api.config.servers.web.urlPathForActions = "api"; 2api.config.servers.web.urlPathForFiles = "public"; 3api.config.servers.web.rootEndpointType = "file";

Say you have an action called ‘status’ (like in a freshly generated actionHero project). Lets start with actionHero’s default config:

api.config.servers.web.urlPathForActions = 'api'; api.config.servers.web.urlPathForFiles = 'public'; api.config.servers.web.rootEndpointType = 'file';

There are 3 ways a client can access actions via the web server.

  • no routing at all and use GET params: server.com/api?action=status
  • with ‘basic’ routing, where the action’s name will respond after the /api path: server.com/api/status
  • or you can modify this with routes. Say you want server.com/api/stuff/statusPage
1exports.routes = { 2 get: [{ path: "/stuff/statusPage", action: "status" }], 3};

The api.config.servers.web.rootEndpointType is "file" which means that the routes you are making are active only under the /api path. If you wanted the route example to become server.com/stuff/statusPage, you would need to change api.config.servers.web.rootEndpointType to be ‘api’. Note that making this change doesn’t stop server.com/api/stuff/statusPage from working as well, as you still have api.config.servers.web.urlPathForActions set to be ‘api’, so both will continue to work.

If you want to shut off access to your action at server.com/api/stuff/statusPage and only allow access via server.com/stuff/statusPage, you can disable api.config.servers.web.urlPathForActions by setting it equal to null (but keeping the api.config.servers.web.rootEndpointType equal to ‘api’).

Hi, I'm Evan

I write about Technology, Software, and Startups. I use my Product Management, Software Engineering, and Leadership skills to build teams that create world-class digital products.

Get in touch