#405 method not allowed

22 messages · Page 1 of 1 (latest)

slate lagoon
#

I've created an application that has an express.js back end and a react front end hosted in Vercel (pro), locall the API calls work but in production I get

   POST https://chatbot-burgerking-git-removedenv-dpms-projects-8cd1083b.vercel.app/api/chat 405 (Method Not Allowed)
half summitBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

indigo heronBOT
# slate lagoon I've created an application that has an express.js back end and a react front en...
Please add more information to your question

Your question currently does not have sufficient information for people to be able to help. Please add more information to help us help you, for example: relevant code snippets, a reproduction repository, and/or more detailed error messages. See more info on how to ask a good question in https://discord.com/channels/752553802359505017/1138338531983491154 and #welcome message

slate lagoon
inland sun
slate lagoon
#

Yep

inland sun
#

show me your express route handler

#

i see what the issue is

slate lagoon
#

🙂 Yes

const express = require('express');
const cors = require('cors');
const axios = require('axios');
require('dotenv').config();

const port = process.env.PORT || 5001;
const app = express();

const corsOptions = {
origin: process.env.FRONTEND_URL || 'http://localhost:3000',
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
optionsSuccessStatus: 204
};

app.use(cors(corsOptions));
app.use(express.json());

app.post('/api/chat', async (req, res) => {
const { prompt } = req.body;
if (!prompt) {
return res.status(400).json({ error: 'Prompt is required' });
}
const data = JSON.stringify({
"endpoint": "SS-chat",
"inputs": {
"chat_messages": [
{
"role": "user",
"content": prompt
}
]
},
"env": {
"OPENAI_API_KEY": process.env.OPENAI_API_KEY
}
});
const config = {
method: 'post',
url: 'https://api.lmnr.ai/v2/endpoint/run',
headers: {
'Content-Type': 'application/json',
'Authorization': Bearer ${process.env.LMNR_API_KEY}
},
data: data
};
try {
const response = await axios(config);
res.json(response.data);
} catch (error) {
console.error('Error calling LMNR API:', error.message);
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response data:', error.response.data);
res.status(error.response.status).json({ error: error.response.data });
} else if (error.request) {
console.error('No response received:', error.request);
res.status(500).json({ error: 'No response received from LMNR API' });
} else {
console.error('Error:', error.message);
res.status(500).json({ error: 'An error occurred while processing your request.' });
}
}
});

app.listen(port, () => {
console.log(Server is running on port ${port});
});

module.exports = app;

inland sun
#

/api/chat is not routed to your express app

#

its a react route

#

solution would be to host your express app in a different vercel project

slate lagoon
#

Ah right so if I understand correctly create a new express/vercel project with code above....

slate lagoon
#

Okay once I do this, I should be able to hit the Express end point from the front end ?

inland sun
#

yes

slate lagoon
#

Okay let me try, thank you.

My QQ is that the end point will only give me a valid repsonse once hit, so i will only know if this works once I've completed it all.

How can I test as i go

inland sun
slate lagoon
#

Hmm so because I want to do a post request I think this is where I get the issue

As I'm getting the error Cannot GET /api/chat

#

Anyway let me put on vercel and send data to it

slate lagoon
#

All good now works