Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  20] [ 1]  / answers: 1 / hits: 20990  / 2 Years ago, fri, january 7, 2022, 12:00:00

I have built a form which I want to be able to send emails, to do this I have attempted to follow this youtube tutorial: https://www.youtube.com/watch?v=_3-By9QfFa0


However I am running into an issue where I am getting the error in the question title which is coming up in my console web browser when trying to submit the form. I realize the issue might have something to do with one of the routes somewhere but I just cant figure it out (unless it's something completely different).


schoolForm.js


const handleSubmit = async(e) => {
e.preventDefault();
try { //I also tried using only: "/send_mail" here like I have in server.js but it didnt work
await axios.post("http://localhost:3000/send_mail", {
name
});
}
catch (error) {
console.log(error);
}
}

server.js


const express = require("express");
const app = express();
require("dotenv").config();
const bodyParser = require("body-parser");
const cors = require("cors");
const nodemailer = require("nodemailer");

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.use(cors());

app.post("/send_mail", cors(), async (req, res) => {
let {text} = req.body;
const transport = nodemailer.createTransport({
host: "smtp-mail.outlook.com",
port: 587,
auth: {
user: "[email protected]",
pass: "password"
},
tls: {
rejectUnauthorized: false
}
});

await transport.sendMail({
from: "[email protected]",
to: "[email protected]",
subject: "subject",
html: `<p>${text}</p>`
})

});

app.listen(4000, () => {
console.log("Server is listening on port 4000");
});

Edit: The error I get in the browser:


enter


Is there anyone that can help me solve this issue? Help would be greatly appreciated!


More From » node.js

 Answers
3

Your server is listening on port 4000. // server.js


app.listen(4000, () => {
console.log("Server is listening on port 4000");
});

You should use below URL for Axios call. // schoolForm.js


await axios.post("http://localhost:4000/send_mail", {    // use port 4000 not 3000
name
});

[#50092] Thursday, November 18, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
simone

Total Points: 558
Total Questions: 96
Total Answers: 99

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
;