Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling directory routes that end both with and without a trailing slash #922

Closed
cbebry opened this issue Jun 10, 2013 · 3 comments · Fixed by #924
Closed

Handling directory routes that end both with and without a trailing slash #922

cbebry opened this issue Jun 10, 2013 · 3 comments · Fixed by #924
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@cbebry
Copy link
Contributor

cbebry commented Jun 10, 2013

I am using a route like follows, in order to serve static content in the public directory:

{ method: 'GET', path: '/{path*}', handler: { directory: { path: './public/', listing:true } } }

There is a subdirectory of public called cartography. Whenever I access the URL http://localhost/cartography/, everything works fine. However, when I access the URL http://localhost/cartography (without trailing slash) all of my relative URLs on the page become 404s because instead of looking for http://localhost/cartography/style.css, it serves http://localhost/style.css.

I admit to being spoiled by web servers like Apache/nginx, and I understand hapi may not necessarily be a replacement for Apache. I just happen to like hapi a lot.

My question is, what is the preferred way to handle this situation with the trailing slash? Is there a way to redirect users from the non-slash to the slash? Do I just not use relative URLs?

Thanks.

@iotaweb
Copy link

iotaweb commented Jun 11, 2013

Hmm... I'm no Hapi expert, but I am not experiencing that problem. In my server options I have the following set:

var options = {
    ...
    files: {
        relativeTo: Path.join(__dirname, 'public')
    },
    ...
};

And in my routes, I have:

    // Add the routes (order does not matter)
    server.route([
        // Routes for index and partials
        { method: 'GET', path: '/', handler: getIndex },
        { method: 'GET', path: '/partials/{path}', handler: getPartial },
        // Routes for static resources, e.g. bower components, css, img, js
        { method: 'GET', path: '/components/{path*}', handler: { directory: { path: './components' } } },
        { method: 'GET', path: '/css/{path*}', handler: { directory: { path: './css' } } },
        { method: 'GET', path: '/img/{path*}', handler: { directory: { path: './img' } } },
        { method: 'GET', path: '/js/{path*}', handler: { directory: { path: './js' } } },
        // All remaining routes - redirect to index
        { method: 'GET', path: '/{path*}', handler: notFound }        
    ]);

I wonder if you're having the trailing slash issue because you've specified listing:true in your route?

@hueniverse
Copy link
Contributor

I'm assuming you are navigating manually to http://localhost/cartography (e.g. you type it, not follow a link from http://localhost/)?

@cbebry
Copy link
Contributor Author

cbebry commented Jun 11, 2013

@hueniverse I first noticed this when sharing the link as http://myhostname/cartography/ on Google Hangouts (with the slash), and the 'linkified' link did not glob the trailing slash. It does as well happen when the user types in the URL manually without the trailing slash. When the user uses the links from the http://myhostname/ page, everything behaves fine because I have control over that.

I understand that it is not the responsibility of the hapi team to be concerned with user error of incorrect typing or Google's very odd link globbing procedure. I'm bringing this up with hopes of finding a workaround or a suggested method so I can close up a potential hole in user experience of my application.

@ghost ghost assigned hueniverse Jun 12, 2013
hueniverse pushed a commit that referenced this issue Jun 12, 2013
@hueniverse hueniverse mentioned this issue Jun 12, 2013
@geek geek closed this as completed in #924 Jun 12, 2013
jmonster pushed a commit to jmonster/hapi that referenced this issue Feb 10, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jan 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants