The styles defined in CSS do not have an impact on EJS templates that have GET route paths stacked

I have a CSS file located in the public directory and two EJS templates in the views directory.

The CSS file works fine when using this route:

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

However, when I created a new route and attempted to style it, the CSS did not work, resulting in the following 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 problematic route where the CSS fails to load:

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')
})

Answer №1

This is the problematic area

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

Modify it to

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

Remember to add the "/" before "styles". It should be included when referencing any file like CSS, js, png, etc.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to manage classNames dynamically in React with Material-UI?

I am wondering how to dynamically add and remove classes from an img tag. My goal is to change the image automatically every 2 seconds, similar to Instagram's signup page. I am struggling to achieve this using the material-ui approach. Below is a snip ...

Notification with jQuery

I am looking to display an alert message when val = -1. However, the issue I am facing is that this message always appears at the bottom of the page regardless of the value of val. if ($val == -1) echo ' <script> $( ...

Modifying button appearance upon clicking using JavaScript

JavaScript: const buttons = document.getElementsByClassName("togglebtn"); for (let i = 0; i < buttons.length; i++) { buttons[i].onclick = function () { this.classList.toggle("active"); } } html: <md-butt ...

Mastering the art of leveraging the raw middleware in the Express framework

I experimented with this block of code: //index.js const express = require("express") const app = express() app.post("/", express.raw(), (req, res) => { console.log(req.body) res.write(req.body.toString()) res.send() }) app.listen(4000) ...

Fade effect on content in Bootstrap carousel

I am currently making some updates to the website mcelroymotors.com. One issue I have encountered while working on the homepage carousel is that the caption only pops up on the first slide, and does not appear on any of the subsequent slides. I would lik ...

showcasing recommended options in the search bar through the use of javaScript

Hey there, I've been working on creating result suggestions for my search bar. The generation process is all set, but I'm facing an issue with displaying the elements on the HTML page. During testing, I keep seeing ${resultItem.name}, and when I ...

I am experiencing issues with my local MongoDB database not properly storing data when using Express and Mongoose

I am encountering an issue where my code is functioning correctly in the console but it is not saving data to the database. Every time I restart the server, the data gets reset. While I can read data from the database without any problem, the issue arise ...

Selecting nested objects in MongoDb

One of the challenges I am facing is dealing with multiple nested objects and lists in my data structure, which looks something like this: { "_id": "5a76be26ca96e22f08af2a19", "testId": "123", "testName": "summerTest", "subjects": [ { "subject ...

File upload not functioning correctly with Angular 7 MEAN stack when using multer middleware

I am currently using the multer middleware for file upload in my Angular mean stack project. However, I am facing an issue where I am unable to retrieve req.file but can successfully access req.body, indicating that the file is not being uploaded. When I c ...

The dimensions of the pop-up window in Chrome's JavaScript are not displaying correctly

My goal is to launch a new chat room window on our website. The dimensions of the chat room are set to 750px x 590px. Below is the link I am using to trigger the javascript popup. <a href="javascript:void(0)" onclick="window.open('http://gamersuni ...

What is the best way to send information to another client (user) using socket.io?

I am facing an issue where data is only being appended to the sender's message body instead of all connected clients. I need it to work for every user who is currently connected. After going through the documentation, it suggests that broadcasting co ...

Utilizing PHP to fetch data from a separate webpage

This is a question that has sparked my curiosity. I am not facing any particular issue that requires an immediate solution nor do I possess the knowledge on how to achieve it. I have been contemplating whether it is feasible to utilize PHP for fetching co ...

What is the process for adjusting the color of the underline in Material UI input fields

I am facing an issue with a Material UI Select component that is on a dark background. I want to change the text and line colors to white just for this particular component, while keeping the rest of the Select instances unchanged. Although I have managed ...

What are some ways I can efficiently load large background images on my website, either through lazy loading or pre

Just dipping my toes into the world of javascript. I'm currently tackling the challenge of lazy loading some large background images on my website. My goal is to have a "loading" gif displayed while the image is being loaded, similar to how it works ...

Develop interactive card collections in Bootstrap that adapt to different screen sizes

I am currently working on developing a responsive card deck using Bootstrap. The goal is to have the card deck display one card per row on mobile, 2 cards per row on tablet, and 3 cards per row on desktop. Below is the snippet of the code I am using: < ...

Issue with establishing socket connection between React and Express.js

When setting up the socket in app.js(backend), I follow this approach: var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser&apos ...

Is there an issue with this particular post request?

Here is the code I wrote to validate whether a POST request can be inserted into a database. When I send a Postman request with the following details: localhost:8080/api/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdb8bea8b ...

Tips for eliminating white frames or borders on the Mapbox canvas map

In my current project using Angular 10, I have integrated Mapbox to display path routes. Following the standard Angular practice of splitting components, I separated the map rendering component and called it into the main map component. However, I encounte ...

Retrieving data from a database collection using Node.js and Express.js

Just getting started with node.js and express.js and I have a question that I'm hoping someone can help me with. In my route, I have the following code: router.get('/:slug', function(req, res) { var subject = req.params.slug; var subject ...

Managing sessions between Node.js and Angular with JSON Web Tokens

I am currently developing an application where NodeJS serves as the backend, handling all business logic and exposing JSON REST services for consumption by the Angular 4 app which acts as a simple client. While this setup seems to be working well, I am fac ...