Axios-如何修复-POST URL net :: ERR_CONNECTION_RESET

问题描述 投票:0回答:1

如何解决此错误消息?

[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放在我的服务器文件夹中。

谢谢你!

node.js express vue.js axios fs
1个回答
0
投票

尝试从您的路线发送回复:

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
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.