ECONNREFUSED in flask app + next.js

piotrpomorskiPRO

7 months ago

Hi, I have deployed my app which uses flask (waitress) for backend and next.js for frontend, while it works perfectly locally, I keep getting ECONNREFUSED even when accessing /api/ to check for "Flask server is running":

@app.route("/", methods=["GET"])
def index():
    logger.debug("Accessing root route")
    return jsonify({"message": "Flask server is running"})

where instead I get "Internal Server Error" (after checking the logs, it's just ECONNREFUSED). Do flask apps need to be configured in some way to work with railway deployment? My next.config.js is specifies destination as:

source: '/api/:path*',
destination: 'http://localhost:8000/:path*',

and I've heard somewhere that it should be using a container's name instead of localhost, however, I know in railway things are not that simple. Do you have any advice there?

The waitress app is just run as:

if __name__ == "__main__":
    logger.debug("Starting Flask server")
    serve(app, host="0.0.0.0", port=8000)
Awaiting User Response

5 Replies

7 months ago

Hello,

You should be starting the next app and the flask app as two separate Railway services under the same project without any proxying.


Status changed to Awaiting User Response railway[bot] 8 months ago


piotrpomorskiPRO

7 months ago

but they are in the same repo, how should I do that? Before I ran fastapi plus next.js and I did not have ECONNREFUSED errors


Status changed to Awaiting Railway Response railway[bot] 8 months ago


7 months ago

I'm assuming this would be an isolated monorepo, and in that case you would set the root directory in the service settings.

If it is not an isolated monorepo, you would want to convert the project into an isolated monorepo.


Status changed to Awaiting User Response railway[bot] 8 months ago


piotrpomorskiPRO

7 months ago

ok, so following this I guess would help: https://docs.railway.app/guides/monorepo#deploying-an-isolated-monorepo but then how would frontend know where backend is? i.e., localhost:8000 is enough?


Status changed to Awaiting Railway Response railway[bot] 8 months ago


7 months ago

Your frontend would call the backends's public domain.


Status changed to Awaiting User Response railway[bot] 8 months ago


ECONNREFUSED in flask app + next.js - Railway Help Station