【问题标题】:Adding Flask server to create-react-app default app, 404 on /dist/bundle.js将 Flask 服务器添加到 create-react-app 默认应用程序,/dist/bundle.js 上的 404
【发布时间】:2019-06-13 06:53:34
【问题描述】:

我正在尝试将烧瓶服务器连接到使用 create-react-app 创建的反应应用程序,并使用 webpack 捆绑它。我主要关注我通过this 项目和this 随附的视频学到的东西,除了我使用默认的 create-react-app 作为起点。

这是我的目录结构

-SimpleApp
--server
----server.py
----__init__.py
--static
----dist
------bundle.js
------styles.css
----node-modules
----src
----package.json
----package-lock.json
----webpack.config.js
----index.html
----__init__.py
--venv

基本上我的“静态”文件夹是默认的 react-app,加上一个 index.html、一个配置文件和一个空的 init.py。

我的 server.py 很简单,我在其中设置了我的 static_folder 和 template_folder

server.py

from flask import Flask, render_template, jsonify

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

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

if __name__ == '__main__':
    app.run()

它调用我的 index.html 看起来像

index.html

<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
    <link rel="stylesheet" href="dist/styles.css">
    <title>simple app</title>
   </head>
 <body>
    <div id="root" />
    <div>Hello</div>
    <script src="dist/bundle.js" type="text/javascript"></script>
   </body>
</html>

我的 webpack.config.js 是

webpack.config.js

const webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

const config = {
    entry:  __dirname + '/src/index.js',
    output: {
        path: path.join(__dirname + '/dist'),
        filename: 'bundle.js',
    },
    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;

package.json

{
  "name": "simpleapp",
  "version": "1.0.0",
  "description": "Fullstack Template",
  "main": "index.js",
  "scripts": {
    "build": "webpack -p --progress --config webpack.config.js",
    "dev-build": "webpack --progress -d --config webpack.config.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "webpack --progress -d --config webpack.config.js --watch"
  },
  "babel": {
    "presets": [
      "es2015",
      "react"
    ]
  },
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.4",
    "extract-text-webpack-plugin": "^2.1.2",
    "file-loader": "^0.11.2",
    "jquery": "^3.2.1",
    "react": "^15.6.1",
    "react-bootstrap": "^0.31.0",
    "react-dom": "^15.6.1",
    "style-loader": "^0.18.2",
    "webpack": "^3.12.0",
    "webpack-cli": "^3.2.1"
  }
}

我运行 npm run watch 和 python server.py 得到

"GET /dist/styles.css HTTP/1.1" 404 -
"GET /dist/bundle.js HTTP/1.1" 404 

这似乎可以追溯到 index.html。我不认为我的配置或包文件与它有任何关系,但我是新手,不想误解。

找不到的文件在我应用的static_folder中,为什么找不到?

我可能遗漏了一些基本概念。如果是这样,请指出我正确的方向

提前致谢!

【问题讨论】:

    标签: reactjs flask webpack http-status-code-404


    【解决方案1】:
    app = Flask(__name__, static_folder='..static/dist', template_folder='../static')
    

    上面可能是混淆的东西,..static../static不一样

    编辑:刚刚看了你链接的教程。我将在下面留下我之前写的答案,但将上面的错字更正为static_folder='../static/dist 可能会更快地解决您的问题。


    就个人而言,我会通过执行以下操作来简化这一点:

    • 请注意,您的 server.py 位于 server 目录中
    • 在这个server目录中创建2个子目录:templatesstatic
    • 将 index.html 移至 server/templates
    • 移动所有静态文件,包括dist子目录到server/static

    现在目录结构应该更像:

    SimpleApp
    └── server
        ├── __init__.py
        ├── server.py
        ├── static
        │   └── dist
        │       ├── bundle.js
        │       └── styles.css
        └── templates
            └── index.html
    

    然后只需使用以下命令初始化应用程序:

    app = Flask(__name__)
    

    这里不需要定义静态和模板目录,因为你已经按照 Flask 期望的默认结构进行了布局。

    尝试在浏览器中点击:http://localhost:5000/static/dist/bundle.js 并确认加载正确。

    然后更新模板以从正确的位置加载这些文件,在模板中使用url_for

    <link rel="stylesheet"
     href="{{ url_for('static', filename='dist/styles.css') }}"
    >
    

    <script type="text/javascript"
     src="{{ url_for('static', filename='dist/bundle.js') }}"
    >
    

    如果您稍后重新生成 bundle.js,您可能还需要调整 webpack 配置以将其放置在新位置。

    【讨论】:

    • 非常感谢,这正是我所需要的。不得不问一个关于一个简单的错字的问题我觉得很傻,但我很高兴也得到了关于目录组织的反馈。弄清楚这可能是一件令人惊讶的奇怪事情。
    • 是的,我发现 Flask 入门有点棘手,因为有很多不同的方式来安排代码,而且在学习教程时经常会错过基础知识,因为每个人的应用程序布局都略有不同。
    • 我将跳回这篇文章,因为现在我在同一个项目中遇到了错误。我按照你最初的建议做了,改变了 static_folder="../static/dist",它起作用了。我决定重建按照您的建议格式化的项目,当我运行“npm run watch”时,我得到了
    • npm 错误! simpleapp@1.0.0 观看:webpack --progress -d --config webpack.config.js --watch npm ERR!退出状态 1 npm ERR! npm 错误!在 fullstack-template@1.0.0 监视脚本中失败。 npm 错误!这可能不是 npm 的问题。上面可能还有额外的日志输出。```
    • 没关系,我想出了这个。在我的配置文件中,我需要将“path: path.join(__dirname + '/dist')”更改为“path: __dirname + '/dist'”。我不知道为什么 path.join 函数会破坏一个应用程序而不破坏另一个应用程序,但至少它可以工作。
    猜你喜欢
    • 2019-06-24
    • 2017-03-03
    • 2018-08-04
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多