Express: using '*' with express.static redirects all the urls

Created on 25 May 2018  路  6Comments  路  Source: expressjs/express

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..

question

All 6 comments

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?

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

@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...

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?

@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".

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/.

Was this page helpful?
0 / 5 - 0 ratings