using '*' with express.static redirects all the urls
See original GitHub issueI 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…
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:6 (2 by maintainers)
@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”.Typically I do something like this to solve the problem you are describing:
Then I point the asset urls to
/static/...
and everything else will end in rendering the index template. Make sense?