我很难将我的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))当我通过postman调用Flask路由时,它工作得很好,但是通过节点服务器,它一直超时。我做错了什么?
提前感谢!
发布于 2020-08-22 05:02:54
我找到了解决这个问题的方法,而且很简单:如果你在公司代理后面工作,就不要使用axios!使用一个简单的promise调用就可以解决这个问题,GET请求也可以工作。
下面这样的代码应该可以做到:
let url = "http://127.0.0.1:5000/api/v1/counts/health-check";
fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(response => console.log(response.json()));我不是100%确定为什么这样做,但我猜这与axios默认使用代理的事实有关。希望这对将来遇到类似问题的人有所帮助!
发布于 2020-08-22 04:00:38
既然您提到了使用Postman,那么您是否尝试过使用它来为API请求生成代码?您可以生成所需的Javascript,并在您的解决方案中尝试。
https://stackoverflow.com/questions/63528786
复制相似问题