Andrew
Andrew

Reputation: 67

Dynamic pages does not support static files in express(NodeJs)

Hello I am working on node application.I set static files using express.It work fine under normal routing.All css and js files working correctly.When I set up dynamic route such as

    router.get('/product/:id',function(req,res,next){
     Product
    .findById({_id:req.params.id},function(err,product){
      if (err) return next(err);
       res.render('main/product',{
         product:product
       });

    });
});

Although the product page working fine it display the result accurate but it does not catch any css or js files which i define on public folder.When i try to open css file its url is http://localhost:3000/product/css/jumbotron.css instead its url should be

http://localhost:3000/css/jumbotron.css

Where am I doing wrong??

Upvotes: 0

Views: 54

Answers (1)

jfriend00
jfriend00

Reputation: 707496

The URLs for your CSS resoruces in your web page are apparently relative URLs. Add a leading / to the front of those URLs so they don't use the path from the web page.

Change this

"css/jumbotron.ss"

to this:

"/css/jumbotron.ss"

in your HTML page.

Without the leading /, your URLs are "page-relative" which means the browser will add the path from the page URL to your CSS URL before requesting it from the server. You don't want that. Adding / to start of the CSS reference will tell the browser to ignore the path of the page.

Upvotes: 1

Related Questions