Search code examples
javascriptnode.jsexpressroutesstatic

Express doesn't load static files from the public folder in routes with multiple parameters


I'm trying to render a page, but the static files are not being loaded by Express only on routes with a single parameter

Static configuration: app.use(express.static(path.resolve(__dirname, "public")));

Route: routes.get("/param/:id", (req, res) => res.render("page"));

Error:

Failed to load resource: the server responded with a status of 404 (Not Found)

Refused to execute script from 'http://localhost:port/param/1/assets/js/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

I've tried everything; maybe it has something to do with the structure of my project?

Repo: projeto-agenda


Solution

    1. Change your static to:
    app.use(express.static(path.join(__dirname, '/public')));
    
    1. Inside your view file change your links to always start with a / eg:
    <script src="/assets/js/bundle.js"></script>