Next.js Discord

Discord Forum

Vercel + Python

Unanswered
Turkish Van posted this in #help-forum
Open in Discord
Turkish VanOP
I would like to include a single python file in my Next.js app but am receiving a 404 error: "POST http://localhost:3000/api/analyzeText 404 (Not Found)". I am currently testing this locally, but received the same error from Vercel after trying to deploy. I created this python file so that I could extract text from a user input and then run some Python code to extract certain words from the input, but after reading the Vercel documentation, I'm just not sure what I'm missing.

I've simplified the index.py page in order to test the route more easily:
app/api/analyzeText/index.py:

from http.server import BaseHTTPRequestHandler, HTTPServer import json class handler(BaseHTTPRequestHandler): def do_POST(self): content_length = int(self.headers['Content-Length']) post_data = self.rfile.read(content_length) data = json.loads(post_data) # Simplified response for testing response = { 'similarity_score': 50, 'missing_keywords': ["example", "keyword"], 'missing_verbs': ["run", "jump"] } self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(json.dumps(response).encode('utf-8'))

And then in my page component, I call it like this:


... const response = await fetch("api/analyzeText", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ textOne: textOne, textTwo: textTwo, }), }); if (response.ok) { .....

Originally, I had a route.js file in the API route - app/api/analyzeText/route.js - that called a python file in my utils folder - utils/analyzeText.py - which worked great when I was running locally, but failed in Vercel. Any tips or steps that I am missing?

0 Replies