Angular-cli with any other server

I'm relatively new to Angular 2, and I'm trying to build an application using the angular-cli system. This works, and I can ng-serve and the application appears. However, it seems like a huge pain in the ass trying to service the application with nothing more than the ng-serve system. In particular, I am trying to run an application created using angular-cli using a Python Flask application. The number of hoops I seem to jump with trying to get this to work makes me crazy! I want to do this because I want to use the REST API with a Python / Flask application that will respond to HTTP service requests from an 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 when using a checkbox?

+4
source share
2 answers

I really “sorted” the problem. I have a directory called "smoke" (short for smoke and mirrors), and inside there I ran the angular-cli command:

ng new static

This created an angular-cli launcher application in a 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()

That way I can go to http: // localhost: 5000 , and the application will serve the Angular application just like "ng serve". Now I can add the REST APIs to my endpoints as I wanted, and you have the services Angular to populate the application.

Arc

+5
source

There is no requirement for the flag to run an external application.

, Flask Angular, , -, Nginx, , ng-serve .

Flask api, . request.get_json() return jsonify(...) JSON.

, HTTP. backend vs. frontend : , , api .

+2

Source: https://habr.com/ru/post/1648418/


All Articles