简体   繁体   English

将Nuxt前端与Flask后端集成的最佳方法

[英]Best method to integrate Nuxt frontend with Flask backend

I am trying to integrate a front-end dashboard built with nuxt with an API back-end built with Flask. 我正在尝试将使用nuxt构建的前端仪表板与使用Flask构建的API后端集成在一起。 Both the front and the back must run on the same local server on the client's closed network. 前端和后端都必须在客户端的封闭网络上的同一本地服务器上运行。 The client wishes to use Flask-Security in order to authenticate users, but this is where the problems start. 客户端希望使用Flask-Security来对用户进行身份验证,但这是问题开始的地方。 Originally, the client wanted to serve static pages in the flask-security templates folder. 最初,客户端希望在flask-security templates文件夹中提供静态页面。

However, because the pages themselves need data from the API in order to load (using nuxt's asyncData), I ran into problems when trying to deploy the front-end files using "npm run generate"/"npm run build" and the only way I could get the front to work together with the back was by deploying the front-end in server-side rendering and running it locally (npm run start) on a different localhost port than the one the API is running on. 但是,由于页面本身需要来自API的数据才能加载(使用nuxt的asyncData),因此在尝试使用“ npm run generate” /“ npm run build”和唯一方法部署前端文件时遇到了问题我可以通过将前端部署在服务器端渲染中并在与API运行的本地主机端口不同的本地主机本地本地运行(npm run start)来使前端与后端一起工作。 So far its been working, but the problem is that now the client wishes to add login to the system, using Flask-Security, but I am running into a brick wall trying to do that... 到目前为止,它一直在工作,但是问题是现在客户端希望使用Flask-Security向系统添加登录名,但是我遇到了尝试这样做的砖墙...

So, I have a few questions - 所以,我有几个问题-

1 - What do you think about would be the ideal way of going about this? 1-您认为这将是解决此问题的理想方法吗? can it be done? 能做到吗

2 - Could you suggest a different method/setting to integrate the front and the back? 2-您能否建议采用其他方法/设置来整合正面和背面? What would be the best practices in this situation ? 在这种情况下,最佳做法是什么? Should the login be done using Flask or using something else? 应该使用Flask还是其他方式完成登录? at the front-end or at the back? 在前端还是后端?

3 - Would you recommend a different login method? 3-您会推荐其他登录方法吗? (just as a note, the front-end hasn't been run with a vue store so far, and I think that is required for authentication through nuxt...) (请注意,到目前为止,前端尚未与vue商店一起运行,我认为这是通过nuxt进行身份验证所必需的...)

I use Vue (not SSR) and am actively maintaining a fork of flask-security - primarily to improve it for use with modern Single page applications. 我使用Vue(不是SSR),并积极维护烧瓶安全性的分支-主要是为了使其与现代单页应用程序一起使用而进行改进。 I have a writeup for using my fork with single page applications here: https://flask-security-too.readthedocs.io/en/latest/spa.html 我在这里有一篇关于将fork与单页应用程序一起使用的文章: https : //flask-security-too.readthedocs.io/en/latest/spa.html

But to answer your question directly - I would separate out your nuxt/vue front end from your backend completely - they can and should be developed and packaged separately - even if they all run in the same container. 但是要直接回答您的问题-我会将您的nuxt / vue前端与后端完全分开-它们可以并且应该分别开发和打包-即使它们都在同一个容器中运行。 My dockerfile downloads my UI repo, runs npm build and copies the entire results to /var/www. 我的dockerfile下载了我的UI仓库,运行npm build并将整个结果复制到/ var / www。 I use nginx to serve the static content, and have it route /api to my flask/uwgi backend. 我使用nginx提供静态内容,并将/ api路由到我的flask / uwgi后端。 The UI communicates via JSON API only - no forms whatsoever. UI仅通过JSON API进行通信-绝不存在任何形式。 So you will be using Flask-Security for managing users (register etc) and for its endpoint/view authorization protections - but not Flask-Securitys html forms. 因此,您将使用Flask-Security来管理用户(注册等)及其端点/视图授权保护-但不会使用Flask-Securitys html表单。

For development - where there is no nginx - there are some tricks needed to make sure redirects and such properly bounce between your backend that is running on a different port (locally) than your front end. 对于开发-在没有nginx的情况下-需要一些技巧来确保重定向以及在与前端不同的端口(本地)上运行的后端之间的正确反弹。 That 'trick' is a new configuration variable in Flask-Security in my fork called: SECURITY_REDIRECT_HOST which will rewrite redirect URLs to a different port. 这个“窍门”是Flask-Security中我分支中的一个新配置变量,名为:SECURITY_REDIRECT_HOST,它将把重定向URL重写到另一个端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM