gpt4 book ai didi

javascript - Webpack 加载资源失败。捆绑.js 404

转载 作者:行者123 更新时间:2023-12-03 00:08:24 25 4
gpt4 key购买 nike

一些背景知识。我一直在关注本教程:https://itnext.io/a-template-for-creating-a-full-stack-web-application-with-flask-npm-webpack-and-reactjs-be2294b111bd并且已经解决标题中的问题一段时间了。

代码编译并且服务器运行,但是在查找bundle.js 时出现 404 错误。

我的文件夹结构是:

.
├── web-app
├── configurations.py
├── __init__.py
├── README.md
├── run.py
└── templates
├── hello
│ ├── __init__.py
│ └── views.py
├── __init__.py
├── public
│ ├── css
│ ├── fonts
│ ├── images
│ └── js
└── static
├── index.html
├── __init__.py
├── js
├── components
├── index.jsx
└── routes.js

我的 webpack.config.js 是:

var path = require('path')
const webpack = require('webpack');
const resolve = require('path').resolve;
const config = {
devtool: 'eval-source-map',
entry: __dirname + '/js/index.jsx',
output:{
path: path.join(__dirname, '/dist'),
filename: 'bundle.js'
},
resolve: {
extensions: ['.js','.jsx','.css']
},
module: {
rules: [
{
test: /\.jsx?/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['react', 'es2015']
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader?modules'
}]
}
};
module.exports = config;

最后,我的index.js是:

<!— index.html —>
<html>
<head>
<meta charset="utf-8">
<!-- Latest compiled and minified bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
<link rel="stylesheet" href="public/css/main.css">
<title>Creating a Full-Stack Python Application with Flask, NPM, React.js and Webpack</title>
</head>
<body>
<div id="content" />
<script src="public/bundle.js" type="text/javascript"></script>
</body>
</html>

我运行“npm run watch”并收到 404 错误,因为它无法找到bundle.js。它寻找的url路径是https://www.mywebsitehere.com/public/bundle.js

我相信我已经将 Flask 设置为在我的 web-app/templates/init.py 文件中查找正确的路径,如下所示:

from flask import Flask

app = Flask(__name__,
static_folder = './public',
template_folder="./static")

from templates.hello.views import hello_blueprint
# register the blueprints
app.register_blueprint(hello_blueprint)

我知道这是很多代码,但经过大量研究和故障排除后,我仍然停留在第一步。任何帮助或指导将不胜感激。

谢谢!

最佳答案

在你的index.html中,scriptsrc指向public/bundle.js,但是你的webpack配置的输出是(包含 webpack 配置的目录)/dist/bundle.js。您的文件夹结构不显示 webpack 配置的位置,但您希望确保配置的输出和 html scriptsrc 属性指向相同的路径.

关于javascript - Webpack 加载资源失败。捆绑.js 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54859041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com