Axios - How to fix - POST URL net::ERR_CONNECTION_RESET

Viewed 5118

How to fix this error message?

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

It works and append data, but I get this error message...

My API looks like this:

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');
            }
        });
    }
});

});

And my Axios POST method looks like this.

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;
      }
    });
}

I use Vue/cli, I separate my client code and my server code. They are on a separate folder. I put Vue/cli in my client folder, and express.js in my server folder.

Thank you in advance!

1 Answers

Try sending a response from your route:

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
    }
});
Related