question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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…

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
vdegennecommented, May 26, 2018

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

1reaction
wesleytoddcommented, May 26, 2018

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - How do I redirect all unmatched urls with Express?
Just uses the order of routes to redirect 1 specific url before it has a catch-all route for everything else. You can put...
Read more >
Redirects With Express - Mastering JS
The res.redirect() function lets you redirect the user to a different URL by sending an HTTP response with status 302.
Read more >
Express.js res.redirect() Function - GeeksforGeeks
The res.redirect() function redirects to the URL derived from the specified path, with specified status, a integer (positive) which ...
Read more >
5.x API - Express.js
The function determines the file to serve by combining req.url with the provided ... For more information, see Serving static files in Express....
Read more >
NodeJS - Redirect URL tutorial (Native and Express) with ...
The http module is a built-in NodeJS module that allows you to create an HTTP server to receive HTTP requests and write responses....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found