Every time I access localhost:3000
, my CSS functions properly. However, when I try to visit localhost:3000/r/test
, the CSS fails to load and I receive the following error message:
Refused to apply style from 'http://localhost:3000/r/styles.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
It seems that whenever I have a route with more than one subpath (not entirely sure if that's the right term), my CSS stops working completely. For instance, these routes function correctly:
app.get('/', (req, res) => {
...
)
app.get('/cats', (req, res) => {
...
)
app.get('/dogs', (req, res) => {
...
)
However, the following routes do not work:
app.get('/cats/test', (req, res) => {
...
)
app.get('/dogs/blah', (req, res) => {
...
)
app.get('/hamster/foo', (req, res) => {
...
)
This is what my index.js file looks like:
const express = require('express');
const path = require('path');
const axios = require('axios');
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname + '/views'));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.render('home');
});
app.get('/r/test', (req, res) => {
res.render('test');
});
app.listen(3000, () => {
console.log(`Listening at http://localhost:3000`);
});
This is what my test.ejs file looks like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Test</title>
</head>
<body>
<h1>Hi<h1>
</body>
</html>
Here is the structure of my project:
Project
|
+-- public
| |
| +--styles.css
|
+-- views
| |
| +-- home.ejs
| +-- test.ejs
|
+--index.js