What could be causing my CSS to malfunction when I include two parameters in app.get?

While developing an express app, I encountered an issue where my CSS code stopped working whenever I added an ":ID" parameter to the URL. It seems like a filepath problem because bootstrap is still loading fine, but on the specific page with the ID parameter, it gives this error message: "Refused to apply style from '' because its MIME type ('text/html') is not supported and strict MIME checking is enabled."

The main.css file is located in my /public folder, but for some reason, it's trying to access it from a folder titled "unapproved".

I've attempted changing the routing order and tweaking the app.use(express.static(__dirname)) code without success.

Here is my app.get:

app.get("/unapproved/:id/", function(req, res){
    var invoiceID = mongoose.mongo.ObjectId(req.params.id);
    InvoiceObj.findById(invoiceID,function(err,foundInvoice){
        if(err){
            console.log(err);
        }else{
            res.render("invoiceScreen",{invoice:foundInvoice});
        }
    });

And here is the HTML snippet:

<% include partials/header %>
<h1><image src="<%= invoice.imageUrl %>"</h1>
<% include partials/footer %>

Lastly, here is my header content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Invoice system</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="main.css">
    <link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet"> 
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
    <script src='https://code.jquery.com/jquery-2.1.4.js'></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</head>
<body>

Answer №1

Perhaps modifying the absolute path in your CSS file could be the solution to resolve the issue.

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

Answer №2

When facing a situation like this, it is advisable to utilize the virtual path for your static files instead of using relative paths, as they may not function smoothly; for example:

Let's consider the following folder structure:

app.js
public
|  +-- main.css
app.use('/static', express.static(__dirname + "/public"))

In your HTML file, ensure that the path is absolute like so:

<link rel="stylesheet" href="/static/main.css">

Answer №3

Forget about using ./ or ../ before your assets (images / css / js) like this: ./css/style.css or css/bootstrap.css ../images/example.png

Instead, you can simply use / like this: /css/style.css or /css/bootraps.css /images/example.png

Using / will direct your application to look from the root directory.

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

Using Promise to manipulate objects and arrays returned from functions

https://i.stack.imgur.com/jvFzC.png router.get('/', function (req, res, next) { var size = req.params.size ? parseInt(req.params.size) : 20; var page = req.params.page ? req.params.page>0 ? (size&(parseInt(req.params.page)-1)) : ...

The issue with the functionality of position absolute in CSS when used with JavaScript

<html> <head> <style> #wrapper{ position: absolute; top : 250px; width: 500px; height: 500px; border: 1px solid red; } .tagging{ position: absolute; border: 1px solid black; width : 20px; height: 30px; } & ...

Manipulating the 'display' attribute with jQuery

Is there a way to show an element that has been hidden using the following CSS? .pds-pd-link { display: none !important; } Can jQuery be used to enable the display of the .pds-pd-link CSS? For example, would $(.pds-pd-link).css("display",""); ...

Utilizing expressjs and nodejs to securely save logged-in sessions in redis

My current setup involves using express/nodejs to manage logged-in sessions stored in redis. Here is the code snippet: app.use(express.session({ key: 'myappname.sid', secret: "Some Secret!!!", store : new RedisStore({ host : ...

How to hide offcanvas navigation bar with one click in Bootstrap 5

There was an issue with a Bootstrap 5 project where the offcanvas menu would remain open after clicking on an anchor link. Is there a way to automatically close the offcanvas menu after clicking on an anchor link? <nav class="navbar fixed-top py ...

What is the best way to adjust the width of a div based on the remaining space in the viewport?

I've created a Wireframe to showcase the concept I'm aiming for. There are 2 screens included to illustrate how the layout should adapt to different screen sizes. Check out the Wireframe here The goal is to have a center-aligned container named ...

express-validator, no validation errors have been found so far

How can I verify if a given string is in the format of an email address? Here's a snippet of code that attempts to do so: req.checkBody('email', 'Invalid email address').isEmail(); var validationErrors = req.validationErrors(); i ...

Most left-aligned icon on the left, most right-aligned icon on the right, and evenly spaced icons in between

Imagine a scenario where you are presented with a questionnaire that allows you to respond using a slider (HTML range element). Below the div containing the range selector (slider), I have arranged icons that need spacing. The icon on the far left should ...

Using Bootstrap to send an HTML form data to a PHP server for processing

When I try to submit an HTML form to a PHP server side on the same page, it works fine when using pure HTML without Bootstrap. However, if I mix in some Bootstrap tags, I encounter issues when trying to submit the form (which is supposed to upload a file a ...

Trying to find the definition of calc(1.25rem + 0.30vw)

While working with Bootstrap, I came across calc(1.25rem + 0.30vw) and I'm unsure of its meaning. This code is being used to design a responsive webpage on a website. Can someone please explain the calculations involved in this? Any help would be gre ...

Error EADDRINUSE occurring consistently when invoking AWS Lambda functions

I set up a Node.js AWS Lambda function that was working perfectly fine through API Gateway until yesterday. Strangely, without any changes on my end, it has stopped functioning. Every time I try to trigger the function, it abruptly terminates with the fol ...

What is the best way to display multiple divs in a single location?

I am facing an issue with displaying different text on mouseover for three buttons. I successfully implemented it for one button, but when I added the other two, it stopped working. Here is the code I used for the first button: Using jQuery- document.get ...

The width of a div element seems to mimic the width of the entire page inexplic

There is a page containing various div elements. HTML: <div class="utilitiesHeader"> <div id="utilitiesLogo"></div> <div id="utilitiesFIO">Name of user</div> </div> <div class="utilitiesContent"> <d ...

Express encountered an issue when trying to upload a file through AngularJS

I am currently facing an issue with uploading a file to express and subsequently to mongoDB. Despite my efforts, when I attempt to upload the file to express, it appears that no data is being passed through the response. I am utilizing ng-file-upload.min. ...

Server blocking access to Javascript files

Having some trouble with my code, it works fine on Localhost but not on Fasthosts server. Seems to be an access issue to a folder on the server, here are the debug messages: GET (http://reggarockaboogie.co.uk/images/gallery/fld01/) 403 (Forbidden) k.cors ...

Printing array of API results in NodeJS using ExpressJS

I have been working with the Twitter API to retrieve tweets and store them in an array. I am looking to print the entire array on my HTML document for display purposes. Excited to see it work! Thanks! Check out the code snippet below: var express = requi ...

Enhance videojs player source with personalized headers

Currently, I have a backend API running on Express that manages a streaming video m3u8 file. The endpoint for this file is: http://localhost:3000/api/stream.m3u8 It is important to note that access to this endpoint requires a valid user token. router r ...

There was an issue with the SidebarController function being undefined, as it was expected to be a function

I'm encountering two issues with my demo: Error: [ng:areq] Argument 'SidebarController' is not a function, received undefined http://errors.angularjs.org/1.2.26/ng/areq?p0=SidebarController&p1=not%20aNaNunction%2C%20got%20undefined ...

Transitioning between modals using Tabler/Bootstrap components in a ReactJS environment

Currently, I am constructing a Tabler dashboard and incorporating some ReactJS components into it. Initially, I used traditional HTML pages along with Jinja2 templates. However, I have now started integrating ReactJS for certain components. I prefer not t ...

Attempting to initiate a call using Client.calls.create() does not begin the call process, yet there are no error messages recorded in either Twilio or

I have deployed a Twilio application on Heroku. My goal is for Twilio to initiate an outgoing call and place the user into a conference when a specific menu option is selected. Below is the code snippet from my handler.js file: const VoiceResponse = requ ...