You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为create-react-app默认项目接入Flask服务器,/dist资源报404错误

Fixing 404 Errors for Static Assets When Integrating Flask with React + Webpack

Let's break down the issues causing those 404 errors for your bundle.js and styles.css, and fix them step by step:

1. Flask Static Folder Configuration & Asset Path Mismatch

Your Flask app is configured to use ../static/dist as the static folder, but your index.html is trying to load assets from dist/ — this creates a double dist/ path that doesn't exist. When you set static_folder='../static/dist', Flask treats this folder as the root for all static asset requests. So a path like /styles.css will point directly to ../static/dist/styles.css, which is exactly where your Webpack output lives.

Corrected server.py:

from flask import Flask, render_template, jsonify
# Fix the static folder path (you had a missing slash) and keep template folder correct
app = Flask(__name__, static_folder='../static/dist', template_folder='../static')

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True) # Enable debug mode for easier troubleshooting

Corrected index.html:

<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
<!-- Remove "dist/" prefix — Flask will look in your static_folder (../static/dist) -->
<link rel="stylesheet" href="/styles.css">
<title>simple app</title>
</head>
<body>
<div id="root" />
<div>Hello</div>
<!-- Same fix here for bundle.js -->
<script src="/bundle.js" type="text/javascript"></script>
</body>
</html>

2. Missing path Module in Webpack Config

Your Webpack config is using path.join() but hasn't imported Node.js's built-in path module — this would cause an error when running Webpack, preventing your bundle from being generated correctly.

Corrected webpack.config.js:

const webpack = require('webpack');
const path = require('path'); // Add this line to import the path module
var ExtractTextPlugin = require('extract-text-webpack-plugin');

const config = {
    entry: path.join(__dirname, '/src/index.js'), // Use path.join for consistent path handling
    output: {
        path: path.join(__dirname, '/dist'), // Fix path concatenation
        filename: 'bundle.js',
        publicPath: '/' // Tell Webpack to use root URL for assets (matches Flask's setup)
    },
    resolve: {
        extensions: [".js", ".jsx", ".css"]
    },
    module: {
        rules: [
            { test: /\.jsx?/, exclude: /node_modules/, use: 'babel-loader' },
            { test: /\.css$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader', }) },
            { test: /\.(png|svg|jpg|gif)$/, use: 'file-loader' }
        ]
    },
    plugins: [ new ExtractTextPlugin('styles.css'), ]
};

module.exports = config;

3. Final Steps to Test

Follow these steps to verify everything works:

  • Navigate to your static folder in the terminal
  • Run npm install to ensure all dependencies are installed (if you haven't already)
  • Run npm run watch to start Webpack in watch mode, which will rebuild your assets when you make changes
  • In a separate terminal, navigate to the server folder and run python server.py
  • Visit http://localhost:5000 in your browser — you should see your React app and the "Hello" text, with no 404 errors in the console

Key Concept to Remember

When using Flask with a bundled front-end:

  • The static_folder points to the directory where your compiled assets (like bundle.js and styles.css) live
  • All static asset URLs in your HTML should use paths relative to this folder (so no extra subfolder prefixes unless you structure your dist folder that way)
  • Webpack's publicPath should match the root URL Flask uses for static assets to ensure paths are consistent during development and production

内容的提问来源于stack exchange,提问作者Zero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:11:44