node.jspugtemplatingtemplating-enginepugjs

How to include a css file in pugjs template


I'm using pugjs for my project.I was unable to load a css file in the pug template. I'm using the following code

index.pug

link(rel="stylesheet", href="views/styles/products.css", type="text/css")

This is my project structure


Solution

  • Express is not going to serve anything that you don't give permission to. You have to give permission by using express.static middleware.

    Put your Static files in a folder then use the express.static middleware like this-

    app.use(express.static(path.join(__dirname, 'public')));
    

    For more details refer to https://expressjs.com/en/starter/static-files.html