docker 获取“AxiosError:connect ECONNREFUSED”在nodejs中调用python API时

gab6jxml  于 2023-03-22  发布在  Docker
关注(0)|答案(1)|浏览(205)

我试图在我的节点脚本中调用我的python脚本中的API,但得到“AxiosError:connect ECONNREFUSED”错误。我已经检查过我确实在localhost:5000上运行flask应用程序,在localhost:5005上运行node应用程序,但不确定为什么连接被拒绝。我做错了什么?最终我打算容器化这些微服务,所以如果你有任何建议,我将不胜感激。
config.js

export const FLASK_MICROSERVICE_URL= "http://localhost:5003/user";

app.js:

import express from "express";
import {FLASK_MICROSERVICE_URL} from './config.js'
import bodyParser from "body-parser";
import cors from "cors";
import axios from 'axios';
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get("/user/:id", async (req, res) => {
  try {
    const userId = req.params.id;
    const url = `${FLASK_MICROSERVICE_URL}/${userId}`;
    const response = await axios.get(url);
    res.send(response.data);
  } catch (error) {
    console.error(error);
    res.sendStatus(500);
  }
});

python文件:

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = //db URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

CORS(app)

class User(db.Model):
    __tablename__ = 'user'

    user_id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), nullable=False)
    email = db.Column(db.String(128), nullable=False)
    contact = db.Column(db.Integer, nullable=False)
    joined_date_time=db.Column(db.DateTime, nullable=False)
    birthdate=db.Column(db.DateTime,nullable=False)
    genre_preferred=db.Column(db.String(64),nullable=False)

    def __init__(self, user_id, username, email, contact, joined_date_time, birthdate, genre_preferred):
        self.user_id = user_id
        self.username = username
        self.email = email
        self.contact = contact
        self.joined_date_time=joined_date_time
        self.birthdate=birthdate
        self.genre_preferred=genre_preferred

    def json(self):
        return {"user_id": self.user_id, "username": self.username, "email": self.email, "contact": self.contact, "joined_date_time":self.joined_date_time, "birthdate":self.birthdate, "genre_preferred":self.genre_preferred}

@app.route("/user")
def get_all():
    users = User.query.all()
    if len(users):
        return jsonify(
            {
                "code": 200,
                "data": {
                    "users": [user.json() for user in users]
                }
            }
        )
    return jsonify(
        {
            "code": 404,
            "message": "There are no books."
        }
    ), 404


@app.route("/user/<string:user_id>")
def find_genre_by_user_id(user_id):
    user = User.query.filter_by(user_id=user_id).first()
    if user:
        genre_preferred=user.json()['genre_preferred']
        return genre_preferred
    return jsonify(
        {
            "code": 404,
            "message": "Preferred Genre not found."
        }
    ), 404


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5003, debug=True)
rkkpypqq

rkkpypqq1#

我不得不在FLASK_MICROSERVICE_URL中使用我的私有IP地址而不是本地主机。

相关问题