0

I have a CSS file in a public directory and 2 EJS templates in views directory.

My CSS file works when i use this route:

app.get('/theresa', (req, res) => {
    res.render('templates/home')
})

But when i was creating a new route and tried to style it, it didn't work => It gives me this Error message:

"Refused to apply style from 'http://localhost:3000/theresa/css/homePage.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled."

This is the route where the CSS doesn't work:

app.get('/theresa/overview', (req, res) => {
    res.render('templates/overview')
})
  <link rel="stylesheet" href="css/homePage.css">
const express = require('express')
const path = require('path')
const ejsMate = require('ejs-mate');
const methodOverride = require('method-override');
const app = express();


app.engine('ejs', ejsMate)
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'))

app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
app.use(express.static(path.join(__dirname, '/public/')))

app.get('/theresa', (req, res) => {
    res.render('templates/home')
})
app.get('/theresa/overview', (req, res) => {
    res.render('templates/overview')
})

app.listen(3000, () => {
    console.log('Serving on port 3000')
})
  • This might help - https://stackoverflow.com/questions/48248832/stylesheet-not-loaded-because-of-mime-type – omegatrix Mar 19 '22 at 15:01

1 Answers1

0

This is where the issue is

<link rel="stylesheet" href="css/homePage.css">

Change it to

<link rel="stylesheet" href="/css/homePage.css">

Take note of the "/" before "CSS". Always include it when serving any file be it CSS, js, png etc