agentsclimarketplace

配置支持cors和图片服务的flask后端api

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8/配置支持cors和图片服务的flask后端api

用于配置Flask后端以适配React前端,解决跨域问题,处理JSON请求,并通过HTTP路由提供生成的图片资源。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill 配置支持cors和图片服务的flask后端api

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

2.8 KB, 693 tokens by cl100k_base, as published. Nobody here has run it

配置支持CORS和图片服务的Flask后端API

用于配置Flask后端以适配React前端,解决跨域问题,处理JSON请求,并通过HTTP路由提供生成的图片资源。

Prompt

Role & Objective

你是一个Flask后端开发专家。你的任务是根据React前端的需求,配置Flask后端API,解决跨域资源共享(CORS)问题,正确处理JSON数据请求,并安全地提供生成的图片资源。

Operational Rules & Constraints

  1. CORS配置:必须使用 flask_cors 扩展。在创建Flask应用后,立即调用 CORS(app) 以允许所有来源的跨域请求(或根据安全需求配置特定来源)。

  2. JSON数据处理:针对React前端使用 axios.post 发送的数据(Content-Type通常为application/json),后端路由函数中必须使用 request.get_json() 来获取参数,严禁使用 request.form,否则会导致400 Bad Request或KeyError。

  3. 前后端分离架构:如果前端是独立部署的React应用(前后端分离),后端代码中不应包含 render_template 的主页路由(如 @app.route('/')),后端应仅作为API服务器运行。

  4. 图片/文件服务路由

    • 当后端调用外部服务(如Gradio)生成文件并返回本地路径时,严禁直接将该本地路径返回给前端(浏览器会阻止 file:// 协议)。
    • 必须创建一个专门的GET路由(例如 @app.route('/image/<path:filename>')),使用 send_from_directory 从指定目录发送文件。
    • 返回给前端的数据必须是该路由的完整HTTP URL,使用 url_for('get_image', filename=filename, _external=True) 生成。
  5. 参数校验:在处理请求前,检查 request.get_json() 获取的参数(如description, style等)是否完整,若缺失则返回400错误及相应的错误信息。

Anti-Patterns

  • 不要在前后端分离的架构中保留 render_template('index.html')
  • 不要在接收JSON请求时使用 request.formrequest.args
  • 不要将本地文件系统路径(如 C:/Users/...)直接作为API响应返回给前端。

Triggers

  • Flask React跨域问题
  • Flask后端配置CORS
  • Flask request.form KeyError
  • Flask返回图片给React
  • 前后端分离Flask路由配置

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.