首页 文章

Angular 2 - appengine python - 传递简单的get请求

提问于
浏览
0

我试图在前端运行角度2,在后端运行appengine python . 所以,我使用dev_appserver.py --port 3030 app.yaml和angular 2作为fronend服务器使用“npm start”启动appengine python作为后端服务器

home_component.ts

import { Component, Injectable } from '@angular/core';
import { Http, Response, RequestOptions, Headers, RequestMethod } from '@angular/http';
import { Observable } from 'rxjs/Observable';



@Component({
    moduleId: module.id,
    selector: 'as-home',
    templateUrl: 'home.html',
    styleUrls: [
        'home.css'
    ]
})

@Injectable()
export class HomeComponent {



     private options = new RequestOptions({ headers: new Headers({ 'Content-Type': 'application/json' }) });

    constructor(private http: Http) { }



    sendPostRequest() {
        return this.http.get("http://localhost:3000/HelloWorld", this.options)
            .map(res => {
                return res.json();
            })
             .catch(this.handleError);         
    }


    private handleError(error: any) {
        // TODO: Use a remote logging insfrastructure
        // TODO: Throw more informative errors
        let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.log(errMsg);
        return Observable.throw(errMsg);
    }

     private extractData(res: Response) {
        return res.json();
    }

后端main.py

# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#from paste.urlparser import StaticURLParser
import json
import webapp2
import time


# app = StaticURLParser("federation-bipedal-hominids")

def AsDict():
  return {"Hello World !": "try now"}

class RestHandler(webapp2.RequestHandler):

  def dispatch(self):
    #time.sleep(1)
    super(RestHandler, self).dispatch()


  def SendJson(self, r):
    self.response.headers['content-type'] = 'text/plain'
    self.response.headers.add_header('Access-Control-Allow-Origin', '*')
    self.response.write(json.dumps(r))

class HelloWorld(RestHandler):

  def post(self):

    r = AsDict()
    self.SendJson(r)

class QueryHandler(RestHandler):

  def get(self):

    r = AsDict()
    self.SendJson(r)



app = webapp2.WSGIApplication([

    ('/HelloWorld', HelloWorld)
    ], debug=True)

的app.yaml

runtime: python27
api_version: 1
threadsafe: true
service: federation-bipedal-hominids

handlers:
- url: /.*
  script: main.app

只是想从angular 2到backend做一个get请求 . 正如我之前提到的,我使用dev_appserver.py --port 3030 app.yaml和前端使用npm start启动后端 . 两台服务器都已启动并正在运行,但我无法从后端获取请求 . 有什么帮助吗?

1 回答

  • 0

    您在端口 3030 上运行 dev_appserver.py ,但您的请求转到端口 3000 ,它们不匹配 .

    您还应该检查 dev_appserver.py 输出中的前几行,这些行指示各个部分正在侦听的实际端口 - 可能有几个,您需要使用 federation-bipedal-hominids 服务侦听的实际端口(可能不会是您指定的那个,具体取决于实际的应用代码) .

    附注:您可能需要一个调度程序文件来帮助将请求路由到适当的服务 . 如果是这样,您还应将其添加到 dev_appserver.py cmdline以更好地复制实际 生产环境 环境 .

相关问题