我想用 Flask 提供一个 React 应用程序,但“static_folder”似乎不起作用。这是我的最小代码:
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__, static_folder="react_app/build")
api = Api(app)
class HelloWorld(Resource):
def get(self):
return app.send_static_file("index.html")
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
在浏览器中,我可以访问http://localhost:5000,它会返回正确的index.html。但是所有静态文件(例如 css 和 js 文件)都会返回 404 错误。例如有一个文件:react_app/build/bundle.css或react_app/build/js/some_name.js。但输入http://localhost:5000/bundle.css或http://localhost:5000/js/some_name.js都会返回 404 未找到错误。
据我了解,如果我指定 static_folder,我应该从根 url 访问该文件夹中的所有文件。我错过了什么/误解了什么?
我对使用快速和渲染模板视图并不陌生,但最近我尝试了一种新的目录结构,这似乎使我的应用程序实例感到困惑。
它不再为我的static bootstrap, css and image文件提供服务,我的视图现在看起来一团糟。
这是我的目录结构
AppName
- node_modules folder
- src (all code live here)
- public (statics)
- css/mycss, bootstrapcss
- js/ myjs, bootstrapjs
- images folder
- models
- app.js (entry point)
Run Code Online (Sandbox Code Playgroud)
静态文件似乎比根目录深两层。在我的app.js文件中,我尝试express static method像这样使用:
app.use( express.static(path.join(__dirname, './src' + '/public')));
Run Code Online (Sandbox Code Playgroud)
并且还尝试过这个:
app.use('/src', express.static(path.join(__dirname, '/public')));
Run Code Online (Sandbox Code Playgroud)
在我看来,例如静态的调用方式如下:
<link href="../public/css/bootstrap.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../public/fonts/glyphicons-halflings-regular.ttf">
<link rel="stylesheet" href="../public/css/styles.css" type="text/css">
<script defer src="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script>
<script src="https://cdn.ckeditor.com/ckeditor5/1.0.0-alpha.2/classic/ckeditor.js"></script>
Run Code Online (Sandbox Code Playgroud)
但仍然无法将这些文件提供给我的观点。我被困在这个问题上。有人可以帮我吗?谢谢
我有一个基于 Symfony 框架的网络应用程序。
我有一个 php-fpm 图像,其中包含所有 php 和 asset 文件。
现在我想通过 nginx 容器提供文件。php部分很简单。但是我如何提供 php-fpm 图像中包含的静态文件呢?
我尝试创建一个由两个容器安装的卷。但是,在首次启动后,docker 将文件从映像复制到卷中后,在我更新映像后,文件不再更新。
我可以在容器启动后将文件从第二个目录复制到卷中以解决此问题,但我不希望产生这种开销。
有谁知道我可以共享静态文件,以便它们可以直接由 nginx 容器提供服务吗?
我的码头工人组成:
version: '3'
services:
nginx:
image: nginx:1.15-alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
- static-content:/var/www/app/web
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
php:
image: pbnl/scotoobo-php:dev
volumes:
- static-content:/var/www/app/web
environment:
- database_host=db
volumes:
static-content:
Run Code Online (Sandbox Code Playgroud)
我的 Dockerfile
FROM php:7.1-fpm
RUN usermod -u …Run Code Online (Sandbox Code Playgroud) 我有一个简单的 FastAPI 应用程序,它提供一个文件,test.html如下app/main.py所示:
@app.get('/')
def index():
return FileResponse('static/test.html')
Run Code Online (Sandbox Code Playgroud)
目录结构是这样的:
app/main.py
app/static/test.html
Run Code Online (Sandbox Code Playgroud)
我可以更改它吗?它可以使用修改后的目录结构(其中app/和static/是同级目录)吗?
我已经尝试过return FileResponse('../static/test.html'),但到目前为止还没有奏效;产生的错误是“运行时错误:路径../static/test.html 处的文件不存在。”
在我的前端应用程序中,我尝试访问在静态文件夹中提供的文件,但遇到了 CORS 问题。我看到的问题是
Access to XMLHttpRequest at 'http://127.0.0.1:5000/static_folder/apple.xml' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)
我的后端 Flask 应用程序有以下设置
app = Flask(__name__, static_url_path='/static_folder', static_folder="static_folder")
返回文件时,我似乎需要将“Access-Control-Allow-Origin”标头添加到我的响应中。Flask 有全局设置方法吗?
我尝试过使用flask_cors但没有成功
from flask_cors import CORS, cross_origin
app = Flask(__name__, static_url_path='/static_folder', static_folder="static_folder")
CORS(app)
Run Code Online (Sandbox Code Playgroud)
通过将SimpleHTTPRequestHandlerCORS 标头设置为
def end_headers (self):
self.send_header('Access-Control-Allow-Origin', '*')
SimpleHTTPRequestHandler.end_headers(self)
Run Code Online (Sandbox Code Playgroud)
但我想使用 Flask 来完成此任务,而不是启动一个简单的 http 服务器。
我正在使用 npm 运行一个 React 应用程序,并且想要修改静态文件的路径。
我修改了 package.json 以包含"homepage": "./app".
正如预期的那样,当我将生产版本与 一起使用时,文件现在位于 .../app/static 中npm run build,但我无法使用开发服务器 ( npm start) 复制此行为。
我怎样才能让开发服务器/app/static也提供服务?
感谢您的任何建议。
有一个名为 django_server 的 Django 项目。当我跑步时
\n\npython manage.py runserver\nRun Code Online (Sandbox Code Playgroud)\n\n页面按预期显示
\n\n\n\n然后,如果我跑
\n\ngunicorn django_server.wsgi:application --bind 0.0.0.0:8000\nRun Code Online (Sandbox Code Playgroud)\n\n页面显示无样式
\n\n\n\n检查控制台,可以看到 .css 和 .js 文件均出现以下错误
\n\n\n\n\n\n\n由于 MIME 类型 (\xe2\x80\ x9ctext / html\xe2\x80\x9d) 不匹配 (X-Content-Type-Options:\n nosniff)。
\n
NOT FOUND: /static/rest_framework/css/bootstrap.min.css\nNOT FOUND: /static/rest_framework/css/bootstrap-tweaks.min.css\n...\nRun Code Online (Sandbox Code Playgroud)\n\n在settings.py中我提到
\n\nBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\nSTATIC_URL = \'/static/\'\nSTATIC_ROOT = os.path.join(BASE_DIR,\'static\')\nRun Code Online (Sandbox Code Playgroud)\n\n这是文件夹结构
\n\n\n\n检查静态文件夹 ( ) 中的权限ls -l,它们显示为
drwxrwxr-x 4 tiago tiago 4096 jun 2 15:49 static\n …Run Code Online (Sandbox Code Playgroud) 我的 SpringBoot 应用程序在一个由 Dockerfile 构建并托管在 AWS ubuntu 实例上的 Docker 容器中运行。一切都工作正常,除了我有一个图像、一个 css 文件和一个 js 文件无法加载。检查页面后,这些文件显示 404 未找到错误。我已使用winscp 将文件上传到我的aws 实例。myApp 文件夹是我的 docker 文件所在的位置以及构建容器的位置。
目录结构是:
myApp
-Dockerfile
-target
-myApp.jar
-src
-main
-java
-[all my code in respective subdirectories]
-resources
-static
-my.js
-my.css
-my.jpg
-templates
-folder1
-html
-html2
-folder2
-html
-html2
Run Code Online (Sandbox Code Playgroud)
我几乎可以肯定我的问题出在 docker 容器和我的 dockerfile 上。
Spring Boot 自动在 /src/main/resources/static 中查找静态文件。我想我的 docker 容器没有这个文件结构。
这是我的 Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install -y openjdk-14-jdk
WORKDIR /usr/local/bin/myApp
ADD . /src/main/resources/static
ADD target/myapp.jar .
ENTRYPOINT …Run Code Online (Sandbox Code Playgroud) 我有一个 Spring Boot 应用程序,其中包含有角度的前端
像这样:
src/main/resources/static/zanori2
ng build在 zanori2 中,我得到了类似的结果:
index.html、index.js、favico.ico 等
我尝试了这个资源句柄:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
/*@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {*/
//registry.addResourceHandler("/**/*")
/*.addResourceLocations("classpath:/static/zanori2/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath,
Resource location) throws IOException {
Resource requestedResource = location.createRelative(resourcePath);
return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
: new ClassPathResource("/static/zanori2/index.html");
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我转到:时localhost:8080/zanori2/index.html,它会让我返回localhost:8080并且 js 文件可以工作。
然而,这很奇怪,因为我不允许共享网址,因为如果我直接访问 localhost:8080 我会得到一个未找到的页面。
并使用其他配置:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer { …Run Code Online (Sandbox Code Playgroud) 我曾多次尝试为从Google App Engine提供的静态文件定义字符集,并且失败了.
文件在文件的标题部分中包含正确的元等效标记:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
Run Code Online (Sandbox Code Playgroud)
但它并没有作为标题传递,浏览器需要从实际文档中获取它.
当然,如果我使用脚本(或Python Google App Engine程序),那么我可以将它作为响应标头正确传递.
Content-Type: text/html; charset=UTF-8
Run Code Online (Sandbox Code Playgroud)
我试图添加到app.yaml文件行:
- url: /
static_files: root/create.html
upload: root/create.html
http_headers:
Content-Type: text/html; charset=UTF-8
Run Code Online (Sandbox Code Playgroud)
但是appcfg.py告诉我:对于URLMap类型的对象,出现了意外的属性"http_headers".在"9oxnet/app.yaml"第41行第5栏中
google-app-engine content-type static-files http-headers app.yaml