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

Please furnish request.body with all necessary details for nodemailer, excluding the email address

I'm currently working on a contact form for my node.js and express web app, utilizing nodemailer. Everything seems to be functioning correctly - the server is recording information and sending it to the specified email address. However, there is an i ...

Create an Angular directive that highlights a div if any of its child inputs have focus

I've developed an Angular directive for a repetitive section containing form elements. My aim is to have the entire section highlighted whenever any input field inside it is focused. template.html <div class="col-md-12 employee-section"> <l ...

Do Bootstrap 3 and Bootstrap 4 - alpha versions maintain backward compatibility with each other at present?

Is backward compatibility maintained between Bootstrap 3 and the alpha version of Bootstrap 4? I am attempting to replace Bootstrap 3 with the new alpha version, but I am experiencing broken styles. Is this a common issue or am I missing something in my a ...

Deploying a nodejs application with angular as the user interface framework using Firebase

Is there a way to set up an express.js application with angular as the front-end framework, multiple route files, and communication between the server and angular via service level API calls, in order to deploy it on firebase using Firebase Hosting? Curre ...

Is it possible to target a specific element within one of two classes that share the same name using CSS or XPath in Webdriver.IO?

Currently, I am using Webdriver.io and facing a challenge in selecting an element within the "text-fields-container" class. This particular element happens to be a password field, and both classes share the same name. Can someone guide me on how to go abou ...

What is the best way to incorporate both images and text in PHP code?

I'm currently working on creating a large image call-to-action (CTA) for my new WordPress website. I've set up a new field group with the type "group" in ACF, and added some functions in PHPStorm. However, none of my images, text, or links are ap ...

How can I prevent the jQuery animation from moving elements by adjusting the border?

I've been working on a small jQuery script that animates the border of images when hovered over. However, I've run into an issue with unwanted repositioning of adjacent images due to the border width increase. $(document).ready(function(e) { ...

Can Okta be integrated with mysql databases?

Is it possible to integrate Okta with MySQL? While I understand that Okta is used for securely storing user accounts, I would also like to have an Id for the users in my own database. ...

Submitting an HTTP POST REQUEST with both an image and text

Is there a way to send an image with text from VueJs to an ExpressJs backend? I've currently implemented two HTTP POST requests for this process. Please note: "this.albumName" and "this.albumDesc" contain text, while the formData variable holds the ...

Error in Knex with the function whereNotExists

I'm encountering some challenges with a Knex route that deals with PostgreSQL. My goal is to insert data into a database only if the item is not already present in the database. I have attempted to use 'whereNotExists' but it doesn't se ...

Nextjs Image Optimization: Accelerating Web Performance with Images

Encountering an issue when trying to display multiple images in nextjs. I noticed that without setting position: relative for the parent element and position: absolute for the child element, along with specific height and width values for the Image itself, ...

It is impossible to perform both actions at the same time

Is it possible to create a progress bar using data attributes in JQuery? Unfortunately, performing both actions simultaneously seems to be an issue. <div class="progressbar"> <div class="progressvalue" data-percent="50"></div> </d ...

Connection Issue: Request from different origin blocked - Absence of CORS header 'Access-Control-Allow-Origin'

I have a basic RESTful API running on ExpressJS and deployed on Bluemix. I encountered a CORS error when trying to access the API from my AngularJS interface. Cross-Origin Request Blocked: The Same Origin Policy prevents reading the remote resource at htt ...

Deleting Content with Node.js

My node.js application utilizes Express for routing and Mongoose for database access. A specific HTTP DELETE request is responsible for deleting an entry in the database if certain conditions are met. .delete(function (req, res) { Movie.findOne( ...

The previous method of using `vue/compiler-sfc ::v-deep` as a combinator has been phased out. Instead, opt for the new syntax `:deep(<

When developing with Nuxt 2, I am encountering numerous [@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead. errors when running npm run dev. After attempting to resolve the issue by changing a ...

Node.js Routing Error: A guide to troubleshooting Express errors

I'm new to NodeJS and Express and I have created a simple CRUD program. However, I am facing an error that is not in my query but in my route. When I try to go to the edit page using <a href='edit/#{data.id}'>Edit Data</a>, the ...

The background image within Bootstrap theme layouts appears to be missing from both the styles and divs

Encountering a peculiar dilemma here. Attempted to apply a background image to a custom style in order to override/extend a bootstrap column style. Additionally, tried embedding a style directly in the div attribute. Strangely, neither method seems to be e ...

"Identifying elements in CSS that do not have any adjacent text elements

I need help identifying a CSS selector that can differentiate between the <var> tags in these two distinct situations: <p><var>Foo</var></p> And <p>Some text <var>Foo</var> and more text</p> Specifi ...

Notification of an uncaught promise being rejected

After creating this function, I keep encountering the Unhandled Promise Rejection Warning. Despite my attempts to refactor it, I just can't seem to get it right. router.post('/authenticate', function (req, res) { User.findOne({username: req ...

Why does nodejs not recognize 'this' as the object containing the function?

After writing the code below in nodejs: var express = require('express'); var app = express(); app.message = "helloworld"; app.get('/check', function (req,res) { res.end("GET request OK"); console.log(this.message); }); app. ...