Deploying svelte kit with flask as backend in one single project

i deployed svelte kit (which works) with API folder that has main.py using flask which is used for all API routes but whenever i use it i get 404 error (from vercel not sveltekit)

here is the top of main.py, folder structure and vercel.json


vercel.json:

{
    "rewrites": [
      { "source": "/api/(.*)", "destination": "/api/main" }
    ]
  }

i noticed in next.js and flask example that they did not use any vercel.json file and dealt with routing in the config of next js, could this be the source of my issue?

i solved the issue by renaming main to index and adding β€œ/api/” before every route in the flask python code and removing the static and template folder stuff in app params

2 Likes

Thanks for coming back and sharing your solution!

For anyone else looking, we’ve also created this community post on other steps you can take to debug 404 errors.

Hey, I’m having related issues @simply-zaki and @pawlean
And I’m an experienced data engineer but very new to anything that touches front end, especially making front end and backend talk to each other.
I’ve got my app working locally, with flask and svelte both spun up in different terminals, and I’ve got it deployed and running without 404, but I can’t get my app to talk to each other, getting 500 errors, and it seems like my flask app isn’t receiving anything.

This seems similar enough to my issue that it would be best to add a comment, but happy to make a seperate post.
How can I fix this, or at least can I confirm that flask is running in vercel?

I want to run svelte, flask, with a sqlite db. Also, let me know if this is even a sensible thing to do.

Here is my project structure


β”œβ”€β”€ README.md
β”œβ”€β”€ __pycache__
β”‚   └── config.cpython-39.pyc
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ __pycache__
β”‚   β”‚   β”œβ”€β”€ __init__.cpython-39.pyc
β”‚   β”‚   β”œβ”€β”€ models.cpython-39.pyc
β”‚   β”‚   └── schemas.cpython-39.pyc
β”‚   β”œβ”€β”€ models.py
β”‚   β”œβ”€β”€ routes
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ __pycache__
β”‚   β”‚   β”œβ”€β”€ drills.py
β”‚   β”‚   └── practice_plans.py
β”‚   └── schemas.py
β”œβ”€β”€ config.py
β”œβ”€β”€ cypress
β”‚   β”œβ”€β”€ downloads
β”‚   β”œβ”€β”€ e2e
β”‚   β”‚   └── drill_creation.cy.js
β”‚   β”œβ”€β”€ fixtures
β”‚   β”‚   └── example.json
β”‚   └── support
β”‚       β”œβ”€β”€ commands.ts
β”‚       └── e2e.ts
β”œβ”€β”€ cypress.config.js
β”œβ”€β”€ instance
β”‚   └── app.db
β”œβ”€β”€ jsconfig.json
β”œβ”€β”€ migrations
β”‚   β”œβ”€β”€ README
β”‚   β”œβ”€β”€ __pycache__
β”‚   β”‚   └── env.cpython-39.pyc
β”‚   β”œβ”€β”€ alembic.ini
β”‚   β”œβ”€β”€ env.py
β”‚   β”œβ”€β”€ script.py.mako
β”‚   └── versions
β”‚       β”œβ”€β”€ __pycache__
β”‚       └── d619d994ebdc_.py
β”œβ”€β”€ package-lock.json
β”œβ”€β”€ package.json
β”œβ”€β”€ playwright.config.js
β”œβ”€β”€ pnpm-lock.yaml
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ app.d.ts
β”‚   β”œβ”€β”€ app.html
β”‚   β”œβ”€β”€ lib
β”‚   β”‚   β”œβ”€β”€ images
β”‚   β”‚   └── vitals.js
β”‚   └── routes
β”‚       β”œβ”€β”€ +layout.server.js
β”‚       β”œβ”€β”€ +layout.svelte
β”‚       β”œβ”€β”€ +page.js
β”‚       β”œβ”€β”€ +page.svelte
β”‚       β”œβ”€β”€ Counter.svelte
β”‚       β”œβ”€β”€ Header.svelte
β”‚       β”œβ”€β”€ api
β”‚       β”œβ”€β”€ drills
β”‚       β”œβ”€β”€ practice-plans
β”‚       └── styles.css
β”œβ”€β”€ static
β”‚   β”œβ”€β”€ favicon.png
β”‚   └── robots.txt
β”œβ”€β”€ svelte.config.js
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ test.js
β”‚   └── test_api.py
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ vercel.json
└── vite.config.js

Here are some file snippets:
app/init.py


app = Flask(__name__)
app.config.from_object('config.Config')

db = SQLAlchemy(app)
ma = Marshmallow(app)
migrate = Migrate(app, db)

CORS(app)

from app.routes.drills import drills_bp
from app.routes.practice_plans import practice_plans_bp

app.register_blueprint(drills_bp, url_prefix='/api/drills')
app.register_blueprint(practice_plans_bp, url_prefix='/api/practice-plans')

app/routes/init.py

(Some duplication here, but shouldn’t cause issues, and doesn’t locally)

from flask import Blueprint

main_bp = Blueprint('main', __name__)

def register_blueprints(app):
    from app.routes.drills import drills_bp
    from app.routes.practice_plans import practice_plans_bp

    app.register_blueprint(drills_bp, url_prefix='/api/drills')
    app.register_blueprint(practice_plans_bp, url_prefix='/api/practice-plans')

app/routes/drills.py


drills_bp = Blueprint('drills', __name__)

@drills_bp.route('/', methods=['POST'])
def create_drill():

and

vercel.json

{
  "rewrites": [
    { "source": "/api/drills/(.*)", "destination": "/api/drills" },
    { "source": "/api/practice-plans/(.*)", "destination": "/api/practice-plans" }
  ]
}

And I’m getting 500 errors whenever I try to use the flask backend.

Error occurred while sending request: TypeError: fetch failed
    at node:internal/deps/undici/undici:13178:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async POST (file:///var/task/.svelte-kit/output/server/entries/endpoints/api/drills/_server.js:9:22)
    at async render_endpoint (file:///var/task/.svelte-kit/output/server/index.js:214:20)
    at async resolve2 (file:///var/task/.svelte-kit/output/server/index.js:3495:22)
    at async respond (file:///var/task/.svelte-kit/output/server/index.js:3384:22)
    at async Server.default (file:///var/task/.svelte-kit/vercel-tmp/index.js:47:3)
    at async Server.<anonymous> (/opt/rust/nodejs.js:8:4242) {
  [cause]: Error: connect ECONNREFUSED 127.0.0.1:5000
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1607:16)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 5000
  }
}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.