*This is my code and I had this error when I try to connect nodejs with react use nodemailer, can you help me to solve it, please? * index.js:1 Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:117)
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: process.env.MAIL_HOST,
port: process.env.MAIL_PORT,
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
});
await transport.sendMail({
from: process.env.MAIL_FROM,
to: "test@test.com",
subject: "test email",
html: `<div className="email" style="
border: 1px solid black;
padding: 20px;
font-family: sans-serif;
line-height: 2;
font-size: 20px;
">
<h2>Here is your email!</h2>
<p>${text}</p>
<p>All the best, Ala</p>
</div>
`,
});
});
app.listen(
(process.env.PORT || 4000,
() => {
console.log("Server is listening on port 4000");
})
);
It seems that something's wrong with your app.listen definition:
Why do you need ``` at the eof?
app.listen(
process.env.PORT || 4000,
() => {
console.log("Server is listening on port 4000");
});