我很难将Node API连接到Flask API,我不断收到Error: connect ETIMEDOUT 10.209.234.2:80
我有一个Node应用程序,它对Flask API进行简单的get请求,如下所示:
var options = {
method: 'GET',
headers: {'Content-Type': 'application/json'},
url: 'http://localhost:5000/api/v1/counts/health-check',
}
await axios(options)
.then(function (resp) {
console.log('SUCCESS, response below')
console.log(resp)
})
.catch(function (error) {
console.log('ERROR, error below')
console.log(error)
})
.then(function() {
console.log("Not an error but see below")
})
我的Flask API路由构建如下:
from flask import Flask, request, jsonify, make_response
from flask_marshmallow import Marshmallow
from marshmallow import fields
import psutil
from datetime import datetime
from flask_cors import CORS
app = Flask(__name__)
ma = Marshmallow(app)
app.config['CORS_HEADERS'] = 'Content-Type'
CORS(app)
@app.route('/api/v1/counts/health-check', methods = ['GET'])
def health_check():
try:
print("Endpoint {} called".format('/api/v1/counts/health-check'))
uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
uptime = uptime.total_seconds()
message = 'OK'
timestamp = datetime.now()
response = jsonify({"uptime": uptime,
"message": message,
"timestamp": timestamp})
return make_response(response, 201)
except Exception as e:
print('The following exception occured: {}'.format(e))
当我通过邮递员打电话给Flask路由时,它工作正常,但通过节点服务器,它一直保持超时。我在做什么错了?
谢谢!
答案 0 :(得分:0)
自从您提到使用Postman以来,您是否尝试过使用它为API请求生成代码?您可以生成所需的Javascript,然后在解决方案中进行尝试。
答案 1 :(得分:0)
我找到了解决方案,这很简单:不要使用axios!使用简单的Promise调用即可解决,而GET请求也可以。
应该执行以下操作:
$POD_NAME
我不是100%知道为什么会这样,但是我想这与axios默认使用代理这一事实有关。希望这对以后有类似问题的人有所帮助!