阅读背景:

在AWS上分离Angular和Django(同时仍然具有方便的本地开发环境)

来源:互联网 

I have a Django project that serves an API with django-rest to an Angular SPA. Currently the Angular is served by Django (it's in the static folder). This is really convenient because it makes working on it very easily locally with just one manage.py run server.

我有一个Django项目,它使用django-rest为Angular SPA提供API。目前,Angular由Django提供(它位于静态文件夹中)。这非常方便,因为它只需一个manage.py运行服务器就可以在本地轻松地进行操作。

On AWS, Django is on an nginx EC2 web server. And all the static files, including angular are on the S3. This works fine for the moment. But I'm sensing that it will soon lead to some difficulties and I would like to separate completely the angular project from the django one (even different github repos).

在AWS上,Django位于nginx EC2 Web服务器上。并且所有静态文件(包括角度)都在S3上。这个目前工作正常。但我感觉它很快会导致一些困难,我想完全将角度项目与django(甚至不同的github回购)分开。

Therefore my infrastructure would look like that :

因此我的基础设施看起来像这样:

  • S3 :
    • Angular App
    • 角度应用程序
    • Django static and media files
    • Django静态和媒体文件
  • S3:Angular App Django静态和媒体文件
  • EC2 :
    • Django app
    • Django app
  • EC2:Django app

As opposed to current :

与当前相反:

  • S3 :
    • Django static files (including Angular App) and media files
    • Django静态文件(包括Angular App)和媒体文件
  • S3:Django静态文件(包括Angular App)和媒体文件
  • EC2 :
    • Django app
    • Django app
  • EC2:Django app

How can I achieve that ? And how this will change the way I launch my server locally ?

我怎样才能做到这一点?这将如何改变我在本地启动服务器的方式?

1 个解决方案

#1


1  

Splitting the two is quite straight forward. Just move all the angularjs code into a separate directory structure and setup the angularjs package as a normal angularjs package. There is an example on the angularjs page: https://docs.angularjs.org/tutorial/step_00.

分裂这两个是非常直截了当的。只需将所有angularjs代码移动到一个单独的目录结构中,并将angularjs包设置为普通的angularjs包。 angularjs页面上有一个例子:https://docs.angularjs.org/tutorial/step_00。

You can just open the index.html in you browser and everything should work.

您可以在浏览器中打开index.html,一切都应该有效。

OR (this is what I'm normally doing) you serve the angularjs app using a webserver. I'm working on osx which has apache2 pre-installed.

或者(这是我正常做的)你使用网络服务器提供angularjs应用程序。我正在研究预先安装了apache2的osx。

The only thing you will have to change is the authentication which will no longer work once you are serving the angularjs app from a different url.

您唯一需要更改的是身份验证,一旦您从其他网址提供angularjs应用程序,该身份验证将不再有效。

You can solve this by moving to Token based authentication (make sure to use https in production).

您可以通过转移到基于令牌的身份验证来解决此问题(确保在生产中使用https)。

Some guidance can be found here:

一些指导可以在这里找到:

Authorization header in AngularJS not working

AngularJS中的授权标头无效

but there are many more hints to be found on stack overflow so just search stack overflow if you get stuck.

但是在堆栈溢出上还有更多的提示,所以如果你遇到困难就会搜索堆栈溢出。


分享到: