Axios POST Request works on localhost but no longer works after deploying on Netlify

Viewed 3658

The axios POST request was working fine on my localhost, but no longer works after deploying my site on Netlify.

The error that I receive is :

Message not sent because of Error: Network Error at createError (webpack:///./node_modules/axios/lib/core/createError.js?:16) at XMLHttpRequest.handleError (webpack:///./node_modules/axios/lib/adapters/xhr.js?:81)**

Am I inputting the wrong url on the request?

Already tried adding CORS on my server.

What am I doing wrong :( please help!!!!

POST Request on Front-End:

formSubmit = e => {
    e.preventDefault();

    this.setState({
        buttonText: "...sending"
    });

    let data = {
        name: this.state.name,
        email: this.state.email,
        phone: this.state.phone,
        message: this.state.message
    };

    axios
        .post("https://www.thejunkim.com/sendmsg", data)
        .then(res => {
            this.setState({ sent: true }, this.resetForm());
        })
        .catch(err => {
            console.log("Message not sent because of ", err);
        });
};

SERVER:

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header(
       "Access-Control-Allow-Headers",
       "Origin, X-Requested-With, Content-Type, Accept"
    );
    next();
});

app.post("/sendmsg", (req, res, next) => {
    let data = req.body;

    let mailOptions = {
        from: '"Nodemailer Contact" <hjk013@gmail.com>',
        to: "hjk013@gmail.com",
        subject: "SOMEONE SENT MSG FROM YOUR PERSONAL WEBSITE",
        html: output
    };

    transporter.sendMail(mailOptions, (err, res) => {
        if (err) {
            console.log(err);
        } else {
            // console.log(JSON.stringify(res));
            console.log("Message sent: %s", res.messageId);
            console.log("Preview URL: %s", nodemailer.getTestMessageUrl(res));
            console.log("res", res);
            res.end();
        }
    });

    res.end();
});
0 Answers
Related