背景:我有两个应用程序。一种是连接到 Mongo 数据库的 Express Node 应用程序。另一个是一个基本的 Web 应用程序,它通过 Fetch API 向 Node 应用程序发出 POST
请求,以从 Mongo 获取数据。
问题:发出 POST
请求后,我在基本 Web 应用程序的控制台中收到以下错误。但是,在我的 Heroku 控制台中,它指出 503
错误是 H12 - 请求超时
。
Fetch cannot load (node app url). No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin (basic web app url) is therefore not allowed access. The response had HTTP status code 503. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
POST
请求似乎确实有效,并且数据已保存到 Mongo 中,但此错误在 Heroku 中被标记为“严重错误”,非常烦人。
Express Node 应用代码
在我的 app.js
文件中,我设置了正确的 header ,以确保其他应用程序可以从不同来源发出请求
app.js
// Add headers so we can make API requests
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
路由/api/api.js
router.post('/users/:url/upload-csv/:csv_name', (req, res) => {
let csv_name = req.params.csv_name;
let csv_string = csv_name+req.body.csv_string;
User.findOne({url: req.params.url})
.then((user) => {
if (user.csv_files.length === 0) {
user.csv_files.push(csv_string);
} else {
let foundExistingCSV = false;
for (var i = 0; i < user.csv_files.length; i++) {
if (user.csv_files[i].includes(csv_name)) {
foundExistingCSV = true;
user.csv_files[i] = csv_string;
break;
}
}
if (!foundExistingCSV) user.csv_files.push(csv_string);
}
user.markModified('csv_files');
user.save();
res.status(204);
})
.catch((err) => {
console.log(err);
res.status(400);
});
});
基本网络应用程序代码
POST
我正在发出请求
utils.js
utils.exportToMongo = functions(table, name) {
var exportPlugin = table.getPlugin('exportFile');
var csv_string = exportPlugin.exportAsString('csv');
// Upload the CSV string and its name to Users DB
fetch(`${utils.fetchUserURL()}/upload-csv/${name}`, {
method: 'POST',
body: JSON.stringify({csv_string: csv_string}),
headers: new Headers({
'Content-Type': 'application/json',
Accept: 'application/json',
})
}).then((res) => {
return {};
}).catch((error) => {
console.log(error);
return {};
});
}
如何消除 503
错误?看来我可能需要在 Express 应用程序中的 POST
请求中添加一些内容来处理请求超时。有任何想法吗?谢谢!
最佳答案
您可以尝试添加 return
并使用更改 status
到 sendStatus
吗?
router.post('/users/:url/upload-csv/:csv_name', (req, res) => {
let csv_name = req.params.csv_name;
let csv_string = csv_name+req.body.csv_string;
return User.findOne({url: req.params.url})
.then((user) => {
if (user.csv_files.length === 0) {
user.csv_files.push(csv_string);
} else {
let foundExistingCSV = false;
for (var i = 0; i < user.csv_files.length; i++) {
if (user.csv_files[i].includes(csv_name)) {
foundExistingCSV = true;
user.csv_files[i] = csv_string;
break;
}
}
if (!foundExistingCSV) user.csv_files.push(csv_string);
}
user.markModified('csv_files');
user.save();
return res.sendStatus(204);
})
.catch((err) => {
console.log(err);
return res.sendStatus(400);
});
});
关于javascript - Node JS Post 请求导致 Heroku 严重错误请求超时 (503),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45359449/