gpt4 book ai didi

node.js - Axios - 如何修复 - POST URL net::ERR_CONNECTION_RESET

转载 作者:行者123 更新时间:2023-12-05 05:04:32 27 4
gpt4 key购买 nike

如何修复此错误消息?

[xhr.js?b50d:178 POST http://localhost:3000/editor/add net::ERR_CONNECTION_RESET][1]

它可以工作并附加数据,但我收到此错误消息...

我的 API 如下所示:

app.js

app.post('/editor/add', function (req, res) {
let articleData;
let textData;
let article = {
title: req.body.title,
content: req.body.content
}

fs.readFile(urlPath, 'utf8', (err, data) => {
if (err) {
console.log('readfile => ' + err);
} else {
articleData = JSON.parse(data);
articleData[article.title] = article.content;
textData = JSON.stringify(articleData, null, 2);

fs.writeFile(urlPath, textData, 'utf8', (err) => {
if (err) {
console.log('write file => ' + err);
} else {
console.log('Finished writing');
}
});
}
});

});

我的 Axios POST 方法如下所示。

editor.vue

submitEditor: function() {
var self = this;
self.$axios({
headers: {
"Content-Type": "application/json"
},
method: "post",
url: "http://localhost:3000/editor/add",
data: {
title: "test5",
content: self.html
}
})
.then(res => {
console.log(res);
})
.catch(error => {
if (!error.response) {
// network error
this.errorStatus = "Error: Network Error";
} else {
this.errorStatus = error.response.data.message;
}
});
}

我使用 Vue/cli,我将客户端代码和服务器代码分开。它们位于单独的文件夹中。我将 Vue/cli 放在我的客户端文件夹中,将 express.js 放在我的服务器文件夹中。

提前致谢!

最佳答案

尝试从您的路线发送响应:

fs.writeFile(urlPath, textData, 'utf8', (err) => {
if (err) {
console.log('write file => ' + err);
} else {
console.log('Finished writing');
res.json({ msg: 'success' }); // send the client something
}
});

关于node.js - Axios - 如何修复 - POST URL net::ERR_CONNECTION_RESET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61053763/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com