Quesofat
Quesofat

Reputation: 1531

Express trying to pull assets from wrong folder

In my express project, when I render the product route with an id, the assets try and load from /products presumably in the public folder (products doesn't exist), but when I render the same html from a slightly different template on /franchino route, it pulls the assets correctly from the public folder.

What's going on and how can I fix this?

Route that doesn't work.

router.get('/product/:slug', function(req, res) {
    //route params
      var slug = req.params.slug;
      var productResp; //scope up api response to pass to render()
      console.log(slug);
    //api call
      Prismic.api("https://prismic.io/api").then(function(api) {
        return api.getByUID('product' , slug);
      }).then(function(response) {

        res.render('product-template', {
          product: response,
        })

      }, function(err) {
        console.log("Something went wrong: ", err);
      });
    });

Error:

GET /product/include/rs-plugin/js/extensions/revolution.extension.migration.min.js 404 0.843 ms - 1226
GET /product/include/rs-plugin/js/extensions/revolution.extension.parallax.min.js 404 1.069 ms - 1226

Route that does work:

router.get('/franchino', function(req, res) {
  res.render('index-rest');
});

App.js

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));

File Structure:

project 
   app.js
   routes
      index.js
   views
      snippets
         snippets
      all templates
   public
      css
      includes 
      js
      etc...

Upvotes: 1

Views: 655

Answers (1)

Quesofat
Quesofat

Reputation: 1531

It looks like whenever there is a multi-tiered route i.e. /products/toys

the line for default asset pulling: app.use(express.static(path.join(__dirname, 'public'))); needs to be changed to app.use('/toys', express.static(__dirname + '/public')); to reflect the route.

Per: Express-js can't GET my static files, why?

Upvotes: 1

Related Questions