简体   繁体   English

我在哪里将前端代码放在后端项目中以及如何/何时运行它?

[英]Where do I put front-end code in my backend project and how/when to run it?

The question is, say I have written a backend REST service using Python, and then some other guy wrote some frontend code in Angular JS. 问题是,我说使用Python编写了一个后端REST服务,然后其他人在Angular JS中编写了一些前端代码。 Is the typical workflow putting them into two separate folders and run them separately? 典型的工作流程是将它们分成两个单独的文件夹并单独运行吗? So the process will look like below 所以这个过程如下所示

python manage.py runserver 

in Python colder and then probably 在Python更冷,然后可能

# in the angular2 folder
npm start

Or should I place all the JS code into some assets folder and when I start my server, all the JS code will run automatically with them? 或者我应该将所有JS代码放入某个资源文件夹中,当我启动服务器时,所有JS代码都会自动运行吗? If so, how should I do it? 如果是这样,我该怎么做?

Another question related is, when is all the JS code sent to users' browsers? 另一个问题是,什么时候将JS代码发送到用户的浏览器? If the app is a client-side rendering app, is the code sent to browser at the first time of server requesting? 如果应用程序是客户端呈现应用程序,那么在服务器首次请求时是否将代码发送到浏览器? How does the server know it should package your JS code and ship it? 服务器如何知道应该打包JS代码并发送它?

Q) The question is, say I have written a backend REST service using Python, and then some other guy wrote some frontend code in Angular JS. 问) 问题是,我说使用Python编写了一个后端REST服务,然后其他人在Angular JS中编写了一些前端代码。 Is the typical workflow putting them into two separate folders and run them separately? 典型的工作流程是将它们分成两个单独的文件夹并单独运行吗?

Both Angular and Python can be run differently as well as together. Angular和Python都可以以不同方式运行,也可以一起运行。 You could choose to place the Angular files (which qualify for all practical purposes as public files) in the public (or related folder) depending on which framework you're using - Django, Flask, Web2py or so on. 您可以选择将Angular文件(有资格用于所有实际目的作为公共文件)放在公共(或相关文件夹)中,具体取决于您使用的是哪种框架--Django,Flask,Web2py等。

You can also choose to run them independently as standalone apps. 您还可以选择将它们作为独立应用程序独立运行。

Depends on your requirement. 取决于您的要求。 Honestly, there are so many ways to answer this question. 老实说,有很多方法可以回答这个问题。

Q)Or should I place all the JS code into some assets folder and when I start my server, all the JS code will run automatically with them? Q)或者我应该将所有JS代码放入某个资源文件夹中,当我启动服务器时,所有JS代码都会自动运行吗? If so, how should I do it? 如果是这样,我该怎么做? If you place everything in your assets folder, then as soon as the home route or any other route is made a request for [from a browser] , the public folder passes on to the browser. 如果您将所有内容放在资源文件夹中,那么只要主页路由或任何其他路由请求[来自浏览器],公用文件夹就会传递给浏览器。 Although I'm not sure if it is the case with server side rendering. 虽然我不确定是否是服务器端渲染的情况。 If you're using Angular 1, I do not think it fits server side rendering although you could use some libraries out there. 如果您正在使用Angular 1,我认为它不适合服务器端渲染,尽管您可以使用一些库。

Q)Another question related is, when is all the JS code sent to users' browsers? 问)另一个相关的问题是,什么时候将JS代码发送到用户的浏览器? If the app is a client-side rendering app, is the code sent to browser at the first time of server requesting? 如果应用程序是客户端呈现应用程序,那么在服务器首次请求时是否将代码发送到浏览器? How does the server know it should package your JS code and ship it? 服务器如何知道应该打包JS代码并发送它? All the files in PUBLIC folder on the server are accessible by a browser. 浏览器可以访问服务器上PUBLIC文件夹中的所有文件。

All of your questions seem to essentially ask the same question. 你所有的问题似乎基本上都提出了同样的问题。

There are many approaches to this problem. 有很多方法可以解决这个问题。

If infrastructure management is difficult for you, than maybe it's easier to place them in the same server. 如果基础设施管理对您来说很困难,那么将它们放在同一台服务器上会更容易。 You can create another directory and place your html that is served to browser with your JavaScript code. 您可以创建另一个目录,并使用您的JavaScript代码将您的html提供给浏览器。

In case you have a good pipeline (which I think it pays for it self) than having another server that serves your application is better. 如果你有一个好的管道(我认为它自己付出代价)比另一台为你的应用程序提供服务的服务器更好。 You can do more things without affecting your service, like caching etc. And your server that runs your service wont be busy serving assets as well. 您可以在不影响服务的情况下执行更多操作,例如缓存等。运行服务的服务器也不会忙于提供服务。

I have a case where we run a node server, which serves the html and javascript to our users, because that way we can do server side rendering. 我有一个案例,我们运行一个节点服务器,它为我们的用户提供html和javascript,因为这样我们就可以进行服务器端渲染。 enter link description here 在此输入链接描述

The flow of code execution will be this : Once the browser of your user hits the server that is serving html and assets, it will download it locally and it will start with JavaScript execution (Parsing, Compiling and Executing). 代码执行流程将是这样的:一旦用户的浏览器点击服务于html和资产的服务器,它将在本地下载它,它将从JavaScript执行(解析,编译和执行)开始。 Your JavaScript app might do some API calls upon initialization or it might not, but your service will be executed only if frontend makes a request. 您的JavaScript应用程序可能会在初始化时执行某些API调用,也可能不会执行,但只有在前端发出请求时才会执行您的服务。

As for CORS it is irrelevant how you serve them, because you can keep them always in the same domain. 至于CORS ,你如何服务它们是无关紧要的,因为你可以让它们永远保持在同一个域中。

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

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