接口:
app.post('/api/save', async (req, res) => {
const data = req.body;
const dynamicSchema = new mongoose.Schema({
...
}, { collection: data.user });
const DynamicModel = mongoose.model(data.user, dynamicSchema);
try {
const newEntry = new DynamicModel(data);
await newEntry.save();
res.status(200).json({ message: 'Data saved successfully' });
} catch (error) {
console.error('Error saving data:', error);
res.status(500).json({ message: 'Error saving data' });
}
});
请求方式:
async function save() {
const data = { ... };
try {
const response = await axios.post('/api/save', data, {headers: {'Content-Type': 'application/json', Accept:'application/json'}});
document.getElementById('saveStatus').textContent = response.data.message;
alert("Data Saved!");
} catch (error) {
document.getElementById('saveStatus').textContent = error.response.data.message;
alert("Save Failed!");
}
}
本地测试时正常:
服务器测试时出现404错误:
服务器上应用由nginx反向代理到8080端口
其他接口在服务器路由正常。如何确定错误?