JavaScript节点js`res.send不是函数错误`

时间:2018-03-30 09:14:44

标签: javascript node.js http

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Password - form</title>
</head>

<body>

  <form>
    <label>Password</label>
    <input type="password" id="passid" />
    <br>
    <input type="submit" value="Sign In" />
    <!-- We also have input type = "button" and events for both >>> onsubmit & onclick. -->
  </form>

  <script>
    function passType() {
      let p = frm.querySelector('#passid');
      let pass = p.value;
      var password = ["p1", "p2", "p3", "p4", "p5"];

      if (password.indexOf(pass) > -1) {
        alert("That's great! Thank you!");
      } else {
        alert("Wrong, try again");
        if (password.indexOf(pass) > -1) {
          alert("It's wrong, you have one more try...");
        } else {
          alert("Get out now!");
          window.close();
        }
      }

    }

    let frm = document.querySelector('form');
    frm.addEventListener('submit', passType);
  </script>

</body>

</html>

使用此代码,我可以接收请求,但我无法回复。它显示var url = require('url'); var http = require('http'); http.createServer(function (req, res) { var url_parts = url.parse(req.url, true); var q = url_parts.query; console.log(q.query); res.setHeader('Content-Type', 'application/json'); res.send(JSON.stringify({ a: 1 })); console.log(q.query); }).listen(8080);

1 个答案:

答案 0 :(得分:12)

您需要res.end代替res.sendres.sendexpress模块的一部分,而不是核心http module的一部分。