首页 > 解决方案 > 当我在 Hyper cmd 上将 app.js 作为节点服务器运行时,会出现“events.js:377”

问题描述

当我在 Hyper 上将 app.js 作为节点服务器运行时,会出现“events.js:377”。还请提及此“events.js:377”的完整描述性含义以及我们如何弄清楚这一点。

我正在编写注册时事通讯,与 .html、.js 和 .css 文件相关的代码附在下面。

我已经多次更改了我的代码模式,但我无法运行 app.js,因为它每次都会崩溃。

如果有人知道解决方案,请回答。

下面给出的是当我将 app.js 作为服务器运行并且它崩溃时显示在超上的错误图像的链接。

错误堆栈

下面给出的代码是 app.js :

    const express = require("express");
    const https = require("https");
    const bodyParser = require("body-parser");
    const request = require("request");
    const app = express();
    app.use(express.static("public"));
    app.use(bodyParser.urlencoded({extended: true}));
    app.get("/", function(req, res){
      res.sendFile(__dirname + "/signup.html");
    })
    app.post("/", function(req, res){
      const firstName = req.body.fName;
      const lastName = req.body.lName;
      const email = req.body.email;
      const data = {
        members:[
          {
            email_address: email,
            status: "subscribed",
            merge_fields: {
              FNAME: firstName,
              LNAME: lastName
            }
          }
        ]
      }
    });
      const jsonData = JSON.stringify("data");
      const url = "https://us5.api.mailchimp.com/3.0/lists/21760b9a041";
    const options = {
      method: "POST",
      auth: "xxxx"
    }
    const requestt = https.request(url, options, function(response) {
        response.on("data", function(data){
          console.log(JSON.parse(data));
        })
    requestt.write(jsonData);
    requestt.end
    });
    app.listen("3000", function(){
      console.log("Server is running on port 3000");
    });

下面给出的代码是 signup.html :

***<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Hugo 0.88.1">
    <title>Signin Template · Bootstrap v5.1</title>
    <link rel="canonical" href="https://getbootstrap.com/docs/5.1/examples/sign-in/">
<!-- When Internet will be ON only then this link of Bootstrap will be applied to your website. -->
    <!-- Bootstrap core CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-uWxY/CJNBR+1zjPWmfnSnVxwRheevXITnMqoEIeG1LJrdI0GlVs/9cVSyPYXdcSF" crossorigin="anonymous">
    <!-- Favicons -->
<link rel="apple-touch-icon" href="/docs/5.1/assets/img/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" href="/docs/5.1/assets/img/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="icon" href="/docs/5.1/assets/img/favicons/favicon-16x16.png" sizes="16x16" type="image/png">
<link rel="manifest" href="/docs/5.1/assets/img/favicons/manifest.json">
<link rel="mask-icon" href="/docs/5.1/assets/img/favicons/safari-pinned-tab.svg" color="#7952b3">
<link rel="icon" href="/docs/5.1/assets/img/favicons/favicon.ico">
<meta name="theme-color" content="#7952b3">
    <!-- Custom styles for this template -->
    <link href="css/styles.css" rel="stylesheet">
  </head>
  <body class="text-center">
  <form class="form-signin" action="/" method="POST">
    <img class="mb-4" src="Image/lab.jpg" alt="" width="72" height="57">
    <h1 class="h3 mb-3 fw-normal">Signup to my Newsletter.</h1>
      <input type="text" name = fName class="form-control middle" id="inputPassword" placeholder="First Name" required>
      <input type="text" name = lName class="form-control top" id="inputEmail" placeholder="Last Name" required autofocus>
      <input type="email"name = email  class="form-control bottom" placeholder="Email" required>
    <button class="w-100 btn btn-lg btn-primary btn-block" type="submit">Sign Me Up!</button>
    <p class="mt-5 mb-3 text-muted">&copy; 2017–2021</p>
  </form>
  
  </body>
</html>***

下面给出的代码是 styles.css :


body {
  height: 100%;
}
body {
  display: flex;
  align-items: center;
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: #f5f5f5;
}
.form-signin {
  width: 100%;
  max-width: 330px;
  padding: 15px;
  margin: auto;
}
.form-signin .checkbox {
  font-weight: 400;
}
.form-signin .form-floating:focus-within {
  z-index: 2;
}
.top {
  margin-bottom: 2px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}
.middle {
  border-radius: 0;
  margin-bottom: 2px;
}
.bottom{
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

标签: javascripthtmlnode.jsapiserver

解决方案


看起来你对 javascript 很陌生。我建议您更好地格式化您的代码,这样您可能会更容易地发现这些错误。

您的错误的确切原因是您在向 mailchimp 发出 API 请求时遇到错误,并且您没有错误处理程序。其次,您可能会收到该错误的原因是因为您只是向他们发送文字字符串“数据”而不是其他任何内容。

正确格式化后的代码如下所示:

app.post("/", function(req, res) {
  const firstName = req.body.fName;
  const lastName = req.body.lName;
  const email = req.body.email;
  const data = {
    members: [{
      email_address: email,
      status: "subscribed",
      merge_fields: {
        FNAME: firstName,
        LNAME: lastName
      }
    }]
  }
});

const jsonData = JSON.stringify("data");
const url = "https://us5.api.mailchimp.com/3.0/lists/21760b9a041";
const options = {
  method: "POST",
  auth: "xxxx"
};

const requestt = https.request(url, options, function(response) {
  response.on("data", function(data) {
    console.log(JSON.parse(data));
  });
  requestt.write(jsonData);
  requestt.end
});

请注意,您发给 mailchimp 的帖子不在您的 app.post 回调处理程序中。由于该代码的位置,它将在您运行代码的那一刻执行。首先,您需要将此代码移动到您的路由处理程序中。

app.post("/", function(req, res) {
  const firstName = req.body.fName;
  const lastName = req.body.lName;
  const email = req.body.email;
  const data = {
    members: [{
      email_address: email,
      status: "subscribed",
      merge_fields: {
        FNAME: firstName,
        LNAME: lastName
      }
    }]
  };

  const jsonData = JSON.stringify("data");
  const url = "https://us5.api.mailchimp.com/3.0/lists/21760b9a041";
  const options = {
    method: "POST",
    auth: "xxxx"
  };

  const requestt = https.request(url, options, function(response) {
    response.on("data", function(data) {
      console.log(JSON.parse(data));
    });
    requestt.write(jsonData);
    requestt.end
  });

});

这将阻止您的代码在您运行代码时执行。

其次,您需要在您的请求方法中添加一个错误处理程序,以便在发生错误时不会导致未处理的错误使您的程序崩溃。这就是导致您在Unhandled 'error' event下面看到消息的原因events:377

const requestt = https.request(url, options, function(response) {
  response.on("data", function(data) {
    console.log(JSON.parse(data));
  });
  response.on("error", function(error) {
    console.error('Error from mailchimp', error);
  });
  requestt.write(jsonData);
  requestt.end
});

最后,您看到的实际错误是socket hang up. 此错误通常是网络错误,这意味着您的客户端对目标服务器的 TCP 请求在完成之前被丢弃/拒绝或以其他方式终止。这可能是在发出请求时没有实际调用 end 函数的结果。具体来说,你有requestt.end,这是一个函数,但你没有调用它,应该是requestt.end();。如果这不能解决问题,那么您可能需要检查运行此代码的计算机的网络连接。

我在您的代码中注意到的另一个错误:

const jsonData = JSON.stringify("data");

您可能打算发送您正在创建的数据对象,在这种情况下,您不想将数据用引号引起来,因为它是变量名,而不是字符串。相反,它应该是:

const jsonData = JSON.stringify(data);


推荐阅读