[Node] Refused to apply style from ... MIME file ~

cautiousยท2021๋…„ 6์›” 20์ผ
0
post-custom-banner

๐Ÿšซ Refused to apply style from 'http://localhost:3000/src/css/common.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

๐Ÿšซ GET http://localhost:3000/index.js net::ERR_ABORTED 404 (Not Found)

์œ„ ์˜ค๋ฅ˜ ๋ฐ ํ•ด๊ฒฐ๋ฒ•์— ๊ด€ํ•ด ์ •๋ฆฌํ•œ๋‹ค.

์˜ค๋ฅ˜ ๋ฐœ์ƒ ๋ฐฐ๊ฒฝ

์„œ๋ฒ„์—์„œ ํ”„๋ก ํŠธ์—”๋“œ์˜ index.js๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ•˜์˜€๋‹ค.
ํ”„๋ก ํŠธ์—”๋“œ์™€ ์„œ๋ฒ„์ธก ํด๋”๋ฅผ ๋ถ„๋ฆฌํ•œ ์ƒํƒœ์˜€์œผ๋ฉฐ, ๋ถˆ๋Ÿฌ์˜จ ํŒŒ์ผ ๋‚ด์šฉ๊ณผ ํด๋” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

ํด๋” ๊ตฌ์กฐ

โ”œโ”€frontend
โ”‚  โ”œโ”€index.js
โ”‚  โ”œโ”€index.html
โ”‚  โ””โ”€src
โ”‚      โ”œโ”€common
โ”‚      โ”œโ”€css
โ”‚      โ””โ”€js
โ”œโ”€server
โ”‚  โ””โ”€index.js

์„œ๋ฒ„ ์ฝ”๋“œ

server/index.js

const express = require('express');
const path = require('path');
const app = express();

app.set('front', process.env.FRONT || path.join(__dirname, '../frontend'));
app.get('/', (req, res) => {
  res.sendFile(path.join(app.get('front'), '/index.html')); // FIXME
});

ํ”„๋ก ํŠธ ์ฝ”๋“œ

frontend/index.html

<!DOCTYPE html>
<html>
  <head>
    <!-- FIXME -->
    <link rel="stylesheet" href="./src/css/common.css" />
  </head>
  <body>
    <canvas id="canvas"></canvas>
    <script type="module" src="./index.js"></script>
  </body>
</html>

๋ฐœ์ƒ ์›์ธ

href๋กœ ์—ฐ๊ฒฐํ•œ ๋งํฌ์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์ถ”์ธก๋œ๋‹ค.
./src/css/common.css ์˜ ์ฃผ์†Œ์ฒด๊ณ„์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๋“ฏ ํ•˜๋‹ค.

index.html์—์„œ importํ•œ ๊ฒฝ๋กœ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ‘œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งคํ•‘๋œ๋‹ค.
ํ˜„์žฌ ์„œ๋ฒ„์—์„œ๋Š” res.get()์œผ๋กœ index.html๋งŒ ์ œ๊ณตํ•ด์ฃผ๊ณ  ์žˆ๋‹ค. localhost/index.js์™€localhost/src/css/common.css ๊ฒฝ๋กœ์— ๋Œ€ํ•ด ์„œ๋ฒ„์—์„œ ์ œ๊ณตํ•˜๋Š” ํŒŒ์ผ์ด ์—†์œผ๋‹ˆ ์Šคํƒ€์ผ๋„ ์ ์šฉํ•  ์ˆ˜ ์—†๊ณ  404์—๋Ÿฌ๋„ ๋œจ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

์œ ์ €๊ฐ€ ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ๋กœ์‹ค์ œ ๊ฒฝ๋กœ์„œ๋ฒ„ ์ œ๊ณต ์—ฌ๋ถ€
localhost:3000frontend/index.htmlO
localhost:3000/index.jsfrontend/index.jsX
localhost:3000/src/css/common.cssfrontend/src/css/common.cssX

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์„œ๋ฒ„์—์„œ ๋‚˜๋จธ์ง€ ํŒŒ์ผ๋“ค๋„ ์ œ๊ณตํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
express.ststic()์„ ์ด์šฉํ•˜์—ฌ ์ •์  ํŒŒ์ผ๋กœ ๋‚˜๋จธ์ง€๋ฅผ ์ œ๊ณตํ•ด์ฃผ์ž.

index.js

const express = require('express');
const path = require('path');

const app = express();
app.set('port', process.env.PORT || 3000);
app.set('front', process.env.FRONT || path.join(__dirname, '../frontend')); 
app.use('/', express.static(path.join(app.get('front'), '/')));
app.listen(app.get('port'), () => {
  console.log(app.get('port'), 'is listening...');
});

์ฐธ๊ณ 

https://stackoverflow.com/questions/55882824/mime-type-text-html-is-not-a-supported-stylesheet

https://developerf1.com/how-to/solving-stylesheet-not-loaded-because-of-mime-type

profile
cautious
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€