0

I started with a full-stack app with Next.js and Typescript frontend and a Python backend. Since we wanted to deploy on Vercel, we migrated all the backend functionality into typescript functions in the /api folder, accessible with:

fetch('api/**foldername**)

The problem is that I have a simple pytorch model so it needs to be Python. I read online that you can have Python serverless vercel functions too.

I need advice on how to make both serverless function types work together.

enter image description here

Here is my folder structure: app/api/routeName/route.ts I tried the same with route.py, but that did not work. I also tried just putting upload.py in the api folder, but that also did not work.

Here is my upload.py file:

from http.server import BaseHTTPRequestHandler


class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write("Hello, worldfbgdfgbdfgbd!".encode("utf-8"))
        return

very simple just to test connectivity.

I tried using a fetch that console logs the response when a button on the frontend is clicked and goes to localhost:3000/api/update.

There isn't much documentation online, and after looking at some example GitHub repos I'm not sure why my case doesn't work.

Any advice is very much appreciated.

1 Answer 1

0

You can add rewrites to your Next.js config file which can rewrite specific routes to your Python API.

For instance, if you were to re-route all your API requests to a Python (Flask) server (running on port 5328):

rewrites: async () => {
    return [
      {
        source: '/api/:path*',
        destination:
          process.env.NODE_ENV === 'development'
            ? 'http://127.0.0.1:5328/api/:path*'
            : '/api/',
      },
    ]
  },

Important to keep in mind, in this example, that API directory is outside of the app/ directory, see Vercel | Examples | Python | Nextjs Flask


For your case, you can re-route it to /api/python/:path* with this structure:

~/
├── app/ 
├── ├── api/
├── api/python/
├── ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.