【问题标题】:How can i solve it index.js:1 Error: Network Error *at createError (createError.js:16) *at XMLHttpRequest.handleError (xhr.js:117我该如何解决它 index.js:1 错误:网络错误 *at createError (createError.js:16) *at XMLHttpRequest.handleError (xhr.js:117
【发布时间】:2021-12-06 16:11:24
【问题描述】:

*这是我的代码,当我尝试将 nodejs 与 react use nodemailer 连接时出现此错误,请您帮我解决它吗? * index.js:1 错误:网络错误 在 createError (createError.js:16) 在 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");
  })
);

【问题讨论】:

    标签: javascript node.js reactjs nodemailer


    【解决方案1】:

    您的 app.listen 定义似乎有问题:

    为什么在 eof 处需要 ```?

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

    【讨论】:

    • 我在写问题时只是输入错误
    • 您在 process.env.PORT 附近有一些额外的括号。你试过我的版本吗? @AlaaStrong
    • 你的版本是什么?? @Bogdan R
    • app.listen( process.env.PORT || 4000, () =&gt; { console.log("Server is listening on port 4000"); });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2019-06-21
    • 2021-06-17
    • 2019-05-23
    • 2021-02-08
    • 2020-12-24
    相关资源
    最近更新 更多