Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
Can't load CSS into express view using a router

I am running into a problem where I cannot load my CSS into a view that gets rendered from a Router. The CSS loads fine for a view rendered from app.js in the root of the project directory

Here is my directory structure,

node_modules/
public/
    styles/
        form.css
        home.css
routes/
    form.js
    product.js
views/
    index.ejs
    savoury.ejs
app.js
package.json

Here is my app.js:

import express from 'express';
import path from 'path';
import productRouter from './routes/product.js';
import formRouter from './routes/form.js';
import dotenv from 'dotenv';
dotenv.config();

const port = process.env.PORT || 3030;
const __dirname = path.resolve();
const app = express();
app.use(express.urlencoded({ extended: true })); // access request body for POST
app.use(express.json()); // process JSON in request body
app.use(express.static(path.join(__dirname,'public'))); // Look up our static files
app.set('view engine','ejs');

// Mount our routers
app.use('/product',productRouter);
app.use('/form',formRouter);

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

app.listen(port, () => { console.log(`Server running on port ${port}`) });

The problem arises, at least to my knowledge, when I try to render a view from a Router called formRouter which is defined in my routes directory as,

import express from 'express';

// Serve our forms from this router
const formRouter = express.Router();

// Render our add savoury form from here
formRouter.get('/add/savoury',(req,res) => {
    res.render('savoury');
});

The view for savoury links the stylesheet like this:

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

I then get the following error in the browser console: Refused to apply style from 'http://localhost:3030/form/add/styles/form.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

I don't know what the problem is. Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Refused to apply style from 'http://localhost:3030/form/add/styles/form.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

So you receiving that error message cause you trying to style the html using a 404 message

But Why are you receiving a 404 error ?

You are receiving a 404 error due to the route or path you calling

if you look closely you can see 'http://localhost:3030/form/add/styles/form.css' is the path it trying to request the css which don't exist

Now in your file path you got

public/
    styles/
        form.css
        home.css

which mean the path you should be calling is

https://localhost:3030/styles/form.css

not http://localhost:3030/form/add/styles/form.css

So to get rid of the error use edit your html to

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

The slash in front of the styles means its calling from the root dir

but omitting it or using ./styles means its from that route folder

If this answer your question remember to mark it answered

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda