与任何其他服务器的角度 [英] Angular-cli with any other server

查看:162
本文介绍了与任何其他服务器的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Angular 2相对来说比较新,而且我正在尝试使用angular-cli系统来构建一个应用程序。这工作,我可以ng和服务和应用程序出现。然而,用ng-serve系统以外的任何东西来尝试和提供应用程序似乎是一个巨大的痛苦。特别是我正在尝试用Python-Flak应用程序构建用angular-cli构建的应用程序。我似乎不得不通过试图让这个工作跳跃的数额让我疯狂!我想这样做是因为我想用Python / Flask应用程序来服务REST API,它将响应来自Angular 2应用程序的HTTP服务请求。



以下是我正在使用的相关版本:

 节点 -  6.2.2 
npm - 2.9.5
angular 2 - rc.4
angular-cli - 1.0.0-beta.9
python - 2.7.5
烧瓶 - 0.10.1

如何在使用Flask时使用Angular应用程序?

解决方案

实际上,sorta解决了这个问题。我有一个名为smoke(烟雾和镜像的简称)的目录,并在那里运行了angular-cli命令:

  ng new static 

这在静态目录中创建了angular-cli start out应用程序。然后我创建了这个(简化的)Python Flask应用程序:

$ $ $ $ $ $ $ $ $ $ $ $
from flask.ext.restful import从gevent导入Api
monkey,pywsgi
monkey.patch_all()
$ b $ def create_app():
app = Flask (press_controller)

#将根文件夹映射到index.html
@ app.route(/)
def home():
return redirect (/index.html)

@ app.route(/< path:path>)
def root(路径):

这是我想通过angular-cli系统为Angular2应用程序创建
的方式,它基本上服务于
static / dist(由angular-cli创建的分发目录)

返回send_from_directory(os.path.join(os.getcwd(),static / dist),路径)

返回应用程序

if __name__ ==__main__:
app = create_app()
server = pywsgi.WSGIServer((0.0.0.0,5000),app)
server.serve_forever()
else:
app = create_app()

这样我就可以导航到 http:/ / localhost:5000 ,应用程序将像ng serve一样提供Angular应用程序。现在我可以添加我的REST API端点,并让Angular服务访问它们来填充应用程序。



Doug


I'm relatively new to Angular 2, and I'm trying to build an app using the angular-cli system. This works and I can ng-serve and the application comes up. However, it seems like a huge pain in the ass to try and serve the application with anything other than the ng-serve system. In particular I'm trying to serve the application built with angular-cli with a Python Flask app. The amount of hoops I'm seemingly having to jump through trying to get this to work is making me crazy! I want to do this because I want to serve a REST API with the Python/Flask app that will respond to the HTTP services requests from the Angular 2 application.

Here are the relevant versions I'm using:

node - 6.2.2
npm - 2.9.5
angular 2 - rc.4
angular-cli - 1.0.0-beta.9
python - 2.7.5
flask - 0.10.1

How can I serve an Angular app while using Flask?

解决方案

I've actually "sorta" solved the problem. I have a directory named "smoke" (short for smoke and mirrors ), and inside there I ran the angular-cli command:

ng new static

This created the angular-cli start out application in the static directory. Then I created this (simplified) Python Flask application:

import os
from flask import Flask, send_from_directory, redirect
from flask.ext.restful import Api
from gevent import monkey, pywsgi
monkey.patch_all()

def create_app():
    app = Flask("press_controller")

    # map the root folder to index.html
    @app.route("/")
    def home():
        return redirect("/index.html")

    @app.route("/<path:path>")
    def root(path):
        """
        This is the cheesy way I figured out to serve the Angular2 app created
        by the angular-cli system. It essentially serves everything from
        static/dist (the distribution directory created by angular-cli)
        """
        return send_from_directory(os.path.join(os.getcwd(), "static/dist"), path)

    return app

if __name__ == "__main__":
    app = create_app()
    server = pywsgi.WSGIServer(("0.0.0.0", 5000), app)
    server.serve_forever()
else:
    app = create_app()

This way I can navigate to http://localhost:5000 and the application will serve up the Angular app just like "ng serve" does. Now I can add in my REST API endpoints as I wanted and have Angular services access them to populate the application.

Doug

这篇关于与任何其他服务器的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆