Skip to content
Advertisement

Unable to load CSS file within nodeJS app

I have an app that I followed from a tutorial for the backend in nodeJS and Express. My connection to MongoDB via Mongoose is working. However, I’ve been trying to add a front-end- at the moment- just a simple html/ejs/css form. My endpoints are loading in localhost but only the html/ejs is rendering. My css file shows as plain code at http://localhost:3000/styles.css but isn’t loading so I’m just getting plain html/ejs. I’m using VS Code. Here’s my relevant code:

App.js:

JavaScript

Product.routes.js

JavaScript

Product.controller.js

JavaScript

Index.ejs

JavaScript

My file structure looks like:

JavaScript

Apologies if it’s obvious- I’m super new to this. As you can see, I’ve tried static files, paths, CORS, and lots of combinations of file routing but I’m getting myself in a big muddle. I’ve considered caching but the css isn’t showing in Chrome, Edge or Incognito mode either. Any help would be appreciated.

Advertisement

Answer

Instead of

JavaScript

write

JavaScript

The public folder name is not part of the stylesheet URL, it is just the folder where express.static looks for files.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement