无法从index.html加载CSS文件

时间:2018-03-30 19:35:13

标签: html css reactjs webpack

我知道这是一个经常被问到的问题。关于它的堆栈溢出我看了很多其他主题,我仍然不知道问题是什么?有些人说它是因为它将文件类型读作text/html MIME类型,但我在index.html中将其声明为text/css并且它仍然在扔我控制台出错。

当我在索引文件中将其更改为text / html时,控制台中的错误消失了,但即使路径正确,我的样式也不会在我的页面上加载?

我在控制台中遇到错误:localhost/:1 Refused to apply style from 'http://localhost:1234/assets/css/styles.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

我在过去2个小时内一直试图解决这个问题,我认为这是我失踪的小事。

这是我的项目结构:

INDEX.HTML



<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/styles.css">
    <title>Portfolio Application</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- Bootstrap JS Library -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" type="text/javascript" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" type="text/javascript" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" type="text/javascript" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  <script type="text/javascript" src="bundle.js"></script></body>
</html>
&#13;
&#13;
&#13;

Webpack.config文件

&#13;
&#13;
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: __dirname + "/src/index.js",
    output: {
        path: __dirname + "/public",
        filename: 'bundle.js',
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    query: {
                        presets: ['env', 'react', 'stage-0']
                    }
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './public/index.html'
        })
    ]
}
&#13;
&#13;
&#13;

服务器文件(Main.js)

&#13;
&#13;
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const cors = require('cors');

const app = express();
const port = 8010;

app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

app.use(cors());
app.use(function(req, res, next){
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
    next();
});

app.use('/', express.static(__dirname + "/../public"));

app.listen(port, () => {
    console.log(`app is listening in on port ${port}`);
});
&#13;
&#13;
&#13;

文件夹结构:

          
  •       
  • public(文件夹)
  •       
              
    • 资产
    •         
    • 字体
    •         
    • 图像
    •         
    • bundle.js
    •         
    • 的index.html
    •       
          
  • 服务器(文件夹)
  •       
              
    • main.js
    •       
          
  • src(文件夹)
  •       
              
    • common(文件夹)
    •         
    • 组件(文件夹)
    •         
    • index.js
    •         
    • routes.js
    •       
          
  • 的package.json
  •       
  • webpack.config.js

0 个答案:

没有答案