Skip to content

Make an any-depth directory parameter also optional  #74

@sholladay

Description

@sholladay

I am struggling to create a route that does what I need due to this constraint set by inert:

Routes using the directory handler must include a path parameter at the end of the path string

Here is my route:

const path = require('path');
const findUp = require('find-up');

module.exports = {
    method  : 'GET',
    path    : '/{appName}/s;id={siteId}/{file*}',
    handler : {
        directory : {
            path : (request) => {
                const { appName } = request.params;
                return findUp.sync(path.join(appName, 'build'));
            },
            redirectToSlash : true,
            listing         : true
        }
    }
};

The above route works perfectly in almost every way. It lets me do a GET to http://localhost/my-app/s;id=xxx/js/my-app.js for example. And it correctly serves the file at /Users/sholladay/Code/personal/my-app/build/js/my-app.js. Awesome.

However, I need the user to be able to GET http://localhost/my-app/s;id=xxx/ and see a directory listing on that build directory. In other words, I need the last parameter to be optional. But using {file?} does not work because then you can't request anything inside of the js directory.

At first I thought "okay, I'll just create another route with a different path but identical handler", along the lines of:

module.exports = {
    method  : 'GET',
    path    : '/{appName}/s;id={siteId*}',
    ...
};

But this doesn't work because {siteId} isn't completely standalone as the last path component. And it wouldn't really do what I want anyway, since siteId would then contain junk.

I imagine I could hack around this by moving the s;id= prefix into route validation instead and using substring() to remove it. But this is getting ugly quick. What I want seems pretty simple: map the path to a specific base. Kind of how h2o2 behaves, but for the local filesystem.

Any thoughts / suggestions? I am posting this here rather than on the discuss repo as this seems like a feature request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug or defect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions