405 method not allowed
Unanswered
South Polar Skua posted this in #help-forum
South Polar SkuaOP
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)
POST https://chatbot-burgerking-git-removedenv-dpms-projects-8cd1083b.vercel.app/api/chat 405 (Method Not Allowed)
20 Replies
South Polar SkuaOP
@South Polar Skua Click to see attachment
u hosting express on vercel?
South Polar SkuaOP
Yep
show me your express route handler
i see what the issue is
South Polar SkuaOP
🙂 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':
},
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(
});
module.exports = app;
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;
/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
South Polar SkuaOP
Ah right so if I understand correctly create a new express/vercel project with code above....
South Polar SkuaOP
Okay once I do this, I should be able to hit the Express end point from the front end ?
yes
South Polar SkuaOP
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
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
@South Polar Skua 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
if it works on localhost it will also work on vercel
South Polar SkuaOP
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
As I'm getting the error Cannot GET /api/chat
Anyway let me put on vercel and send data to it
South Polar SkuaOP
All good now works
@South Polar Skua All good now works
Sloth bear
How to solved?