The specified MIME type (text/html) is not compatible with stylesheets

When attempting to link a css file to my express-handlebars file, I encountered the following error:

The style from 'http://localhost:4000/cs366/style/draft.css' was refused due to its MIME type ('text/html') not being a supported stylesheet MIME type, with strict MIME checking enabled.

Even though my static directory is set up correctly and the css is working on home.handlebars, it is not being applied to fantasyDraft.handlebars

Directory Structure

project
|-- public
|   `-- style
|       |-- home.css
|       `-- draft.css
|-- Fantasy
|   `-- fantasyRouting.js
|-- views
|   |-- fantasyDraft.handlebars
|   `-- home.handlebars
|-- app.js
`-- connect.js

App.js

const express = require('express');
var exphbs  = require('express-handlebars');
const db = require('./connect'); //connnect to db
const path = require('path');
const app = express();


//middleware
app.use(express.urlencoded({extended: false}));

//static folder
app.use(express.static(path.join(__dirname, '/public')))

//handlebars middleware
app.engine('handlebars', exphbs({defaultLayout: null}));
app.set('view engine', 'handlebars');


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

//fantasy
app.use('/cs366/fantasy/start', require('./fantasy/fantasyRouting'));

fantasyRouting.JS - Routes management

const express = require('express');
const router = express.Router();

router.post('/', (req, res) => {



    players = [
        {
            id: 1,
            name: 'Alex Johnson',
            position: 'p10',
            ovr: 60
        },
        {
            id: 2,
            name: 'Carl Erdman',
            position: 'p2',
            ovr: 76
        },
        {
            id: 3,
            name: 'Joe Enslin',
            position: 'p1',
            ovr: 29
        }
    ]

    res.render('fantasyDraft', {
        players: players
    });



});

module.exports = router;

fantasyDraft.handlebars

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="../style/draft.css">
    <title>Document</title>
</head>
<body>
    <div class='draft-header'>
        <h1>Drafting</h1>
    </div>

    <div class="tables-container">

        <!-- Team table -->
        <table id='team-table'>
            <th>Name</th><th>OVR</th><th>Postion</th>
            <tbody>

            </tbody>
        </table>

        <!-- Draft table -->
        <table id='draft-table'>
            <th>Name</th><th>OVR</th><th>Postion</th>
            <tbody>
                {{#each players}}
                    <tr><td>{{this.name}}</td><td>{{this.ovr}}</td><td>{{this.position}}</td><td><button type='submit' id='draft-player' index={{this.id}}>Draft</button></td></tr>
                {{/each}}
            </tbody>
        </table>
    </div>
</body>
</html>

home.handlebars

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="../style/home.css">
    <title>Homepage</title>
</head>
<body>

    <h1>CS-366 Soccer</h1>

    <div class='form-container'>
        <form action="/cs366/submit" method="POST">
            <input type="text" name='input' placeholder="Enter a player or team">
            <button type='submit'>Search</button>
        </form>
    </div>

    <div class='fantasy'>
        <p>stuff</p>
        <form action="/cs366/fantasy/start" method="POST">
            <input type="text" id='season' name='teamName' placeholder="Enter your team name">
            <button type='submit' id='season'>Start Season</button>
        </form>

    </div>



</body>

</html>

Answer №1

The reason your html file can't find the correct css is because of the relative path. When serving express files as static, the path is relative to the express route, not the actual folder path.

Since everything in your public folder is served as static, it's best to use absolute paths for easier navigation:

<link rel="stylesheet" href="/style/draft.css" />

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

Exploring the Seas: A Guide to Safely Packaging Your Sails Application

I've been working on a project using Sails. Currently, in order to move it to another server, I have to follow these 3 steps: Step1: Check out the source code. Step2: Execute npm install to fetch dependencies Step3: Deploy the Sails app by runnin ...

issues with functionality in purecss drop down menu

I have been struggling to create a vertical drop-down menu using the purecss library without success. purecss.io/menus/ This is what my CSS code looks like: <div class="pure-menu custom-restricted-width"> <ul class="pure-menu-list"> ...

Handling Forms with Node and Express

I'm currently following a guide to create a node/express application using the jade template engine. I've encountered a 404 error when trying to submit a form. In my routing, I have this line: app.post('sign_up', sign_up); which is caus ...

Error: The specified path in the MEAN stack must be either a string or Buffer

I am currently utilizing Angular 5 on the front-end, Node for back-end operations, and MongoDB as the database. My current challenge involves attempting to save an image to the database, but I keep encountering an error. Determining whether the issue lies ...

What is the process for storing and executing a stored procedure in a database?

I recently created a stored procedure for Postgres as part of the backend development work being done in Node.js. Now, I need to upload this stored procedure to our development server. The usual process involves executing the 'CREATE PROCEDURE...&apos ...

Elegant bespoke input box

I am looking to create a customized input text feature similar to StackOverflow's tag editor, but with some minor differences. The goal is for the input field to function like a regular text input until a word is enclosed in curly brackets. Once enclo ...

Troubleshooting the lack of deep linking functionality in an AngularJS web application when using Node Express server

(Update: The problem has been successfully solved. Check the end of the question for details) I am currently facing a seemingly trivial issue that is causing me a great deal of frustration as I struggle to find a solution: After scaffolding an Angular ap ...

Exploring Node.js with a straightforward illustration and tackling the EPERM error

const server = require('http').createServer(function(req, res){ }); server.listen(8080); This code snippet is causing an error to be displayed in the console: $ node node_server.js node.js:201 throw e; // process.nextTick error, or &apos ...

How can testing a callback in node.js with sinon be approached if the callback is not given as a parameter?

var retrievedData = function (request, response, next) { var dataToReturn; model.getData(function (error, result) { dataToReturn = result[0]; request.headers[constants.DATA] = dataToReturn; next(); } ...

The shadow effects and color overlays do not seem to be functioning properly in Mozilla Firefox

I have designed a popup registration form using the Bootstrap modal class. To ensure form validation, I have integrated some jQuery validation engine functionality. Additionally, I customized the appearance by adding a box shadow, adjusting the background ...

Top method for incorporating nested sub documents within mongoDB

Within my system, I have established a connection between a user and a message model. The relationship is clear: a user can possess multiple messages. Purpose: I am seeking the most effective and elegant approach to include a new message and associate it ...

Nested async waterfall within an async series

Can the use of async.waterfall be nested inside of async.series? For instance, let's say there is a module named api that contains two methods, api.get and api.post, along with another module called resizer that downloads an image from an S3 bucket, ...

If the servers experience downtime, Redis Node.js (Express) seamlessly transitions to the backup database

I am exploring ways to implement error handling in Redis with NodeJS. My goal is for the code to be able to detect if the Redis server is offline, and if so, automatically switch to fetching data from the Database instead. I am currently using Redis vers ...

Problems with installing Homebrew

I've been encountering some issues with brew installs. Here's what I've done so far: First, I uninstalled homebrew using the following command: $ rm -rf /usr/local/Cellar /usr/local/.git && brew cleanup Then, I reinstalled homebr ...

Why is the message ID missing in Firebase Cloud Messaging?

I am currently in the process of upgrading from legacy HTTP to v1 using the NPM firebase-admin module. The issue I am facing is that when I send a notification, there are no errors thrown, but neither do I receive a message-id and the notification does not ...

Interested in mastering BootStrap for Angularjs?

As a PHP developer with a newfound interest in JavaScript, I took it upon myself to learn AngularJS and jQuery. However, I recently discovered that simply mastering Angular is not enough - Bootstrap is also necessary. My only issue is my fear of CSS; handl ...

What is the best location to store a ReactJS file?

As I dive into learning ReactJS Components, here is the code I am working with. Could someone please advise me on where to save this file or which folder I should keep it in? <body> <div id="anmol"></div> <script src="https: ...

Contrasting characteristics of a node.js server built with http.createServer versus one created with express

Could you explain the distinction between setting up a server with the http module versus configuring a server with the express framework in Node.js? Appreciate it. ...

How can you use the MongoDB Aggregation Framework to filter by a range of dates, group results by individual days, and calculate the average value for each day

I'm currently exploring the possibilities of MongoDB's Aggregation Framework and would appreciate some assistance in enhancing this query to achieve the following objectives: Retrieve Records with Dates falling within a specified range Organize ...

Having trouble getting MongoDB to connect correctly to my server (using node.js and express), any help would be greatly appreciated

Whenever I make an API request using Hoppscotch, I encounter a terminal error along with status code 500 on Hoppscotch. Despite my efforts to fix the issue, it persists. Below is the code snippet that I am currently working with: var mongodb = require(&apo ...