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

using '*' with express.static redirects all the urls #3660

Closed
vdegenne opened this issue May 25, 2018 · 6 comments
Closed

using '*' with express.static redirects all the urls #3660

vdegenne opened this issue May 25, 2018 · 6 comments
Labels

Comments

@vdegenne
Copy link

vdegenne commented May 25, 2018

I am facing quite an unexpected behavior. I am using express for my back-end REST api and to serve the statics which is a Polymer front-end application. If I use

app.use(express.static('public/build'));
// or
app.use('/', express.static('public/build'));

and start my node app, and request http://localhost:3000/, the application works normally. Again it is a Polymer application that means if I click on local links the page is not refreshing because it's using the history api, so for instance if I click <a href="/about">about</a> the application will just load the appropriate view and will request appropriate data using an Ajax mechanism. That means the application seems to work normally but If I refresh the page say while the URL is http://localhost:3000/about express won't have a clue what /about route is and will just output Cannot GET /about on the page.

My Polymer apps need to be able to handle every URLs because there is also a 404 view, so it's ok to direct all routes to the same statics. My first thought was to write :

app.use('*', express.static('public/build'));

But now I encounter this issue where express tries to redirect (301) all my URLs,

it redirects all URLs even the static files like scripts and try to add a trailing slashes, which results in a complete fails to load my application..

@dougwilson
Copy link
Contributor

This is because the star will slurp up the entire URL and now express.static thinks every request is to the base URL. What are you expecting the behavior to be when you do that?

@vdegenne
Copy link
Author

Sorry to post what seems to be a stackoverflow question. At first I thought that was an unexpected behavior and decided to open this issue but now I understand why I got these redirects. Before closing this thread I'd be glad to hear if you know any way to redirect all URLs to the same base URL. What I am trying to achieve is to redirect all requested URLs to the same index file, except for resources files (.js, .css, .png, .jpg, etc...). Thanks

@vdegenne
Copy link
Author

@dougwilson Ok I am closing now as I make a fool of myself. I just understand that express.static is used for static files... and that I should use routes to serve the index.html...

@wesleytodd
Copy link
Member

Typically I do something like this to solve the problem you are describing:

app.use('/static', static('.'))
app.use((req, res) => {
  res.render('index');
})

Then I point the asset urls to /static/... and everything else will end in rendering the index template. Make sense?

@vdegenne
Copy link
Author

@wesleytodd thanks for your answer, this is exactly what I found out today. I knew I was doing something wrong, because I was dynamically creating routes. I thought static was for... static routes ? But It is more right to think as "static files".

@polomoshnov
Copy link

Almost a whole year has passed since this issue arose but now something similar is happening to me. So, after reading here and there I've come to the conclusion that if you need a path that covers every URL that starts with it you just write your path without an asterisk as in the following example: use('/user-assets/', express.static('/data/user-assets/') - perfectly works with URL http://example.com/user-assets/john123/notes-12.05.19.txt.

As first, I was using '/user-assets/*' mistakenly thinking that would cover all files in my directory /data/user-assets and its subdirectories. But that was just redirecting URLs (301 Moved Permanently) adding a trailing slash to them as though they were directories and not files: http://example.com/user-assets/john123/notes-12.05.19.txt got redirected to http://example.com/user-assets/john123/notes-12.05.19.txt/.

@expressjs expressjs locked as resolved and limited conversation to collaborators May 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants