无法在 nodeJs 中接收 POST 数据

Can not receive POST data in nodeJs

我是nodeJs的初学者。我只想在 nodeJs 中接收 GET 和 POST 数据。

我尝试并成功获取数据,但在 POST 数据中我收到错误 ReferenceError: setImmediate is not defined。 当我 运行 server.js 使用 $ node server.js 时,它将在 3000 端口侦听并重定向到 index.html 页面并通过错误。

我做了 Google 但没有找到解决我的错误的方法。

我正在附上代码。

server.js

var express        =         require("express");
var bodyParser     =         require("body-parser");
var app            =         express();

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

app.get('/',function(req,res){
  res.sendFile(__dirname+"/index.html");
});
app.post('/login',function(req,res){
  var user_name=req.body.user;
  var password=req.body.password;
  console.log("User name = "+user_name+", password is "+password);
  res.end("yes");
});
app.listen(3000,function(){
  console.log("Started on PORT 3000");
})

package.json

 {
  "dependencies":
  {
    "express":"*",
    "body-parser":"*"
  }
}

index.html

 <html>
  <head>
    <title>Simple login</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">  </script>
    <script>
      $(document).ready(function(){
        var user,pass;
        $("#submit").click(function(){
          user=$("#user").val();
          pass=$("#password").val();
          $.post("http://localhost:3000/login",{user: user,password: pass}, function(data){
            if(data==='done')
              {
                alert("login success");
              }
          });
        });
      });
    </script>
  </head>
  <body>
    <h1>Hello people !</h1>
    <input type="TEXT" id="user" size="40"><br>
    <input type="password" id="password" size="40"><br>
    <input type="button" id="submit" value="Submit">
  </body>
</html>

我遇到的错误:

Started on PORT 3000

/var/www/html/nodeJs/Example-4/node_modules/express/lib/response.js:1013
    setImmediate(function () {
    ^
ReferenceError: setImmediate is not defined
    at Array.onfinish [as 0] (/var/www/html/nodeJs/Example-4/node_modules/express/lib/response.js:1013:5)
    at listener (/var/www/html/nodeJs/Example-4/node_modules/express/node_modules/on-finished/index.js:169:15)
    at onFinish (/var/www/html/nodeJs/Example-4/node_modules/express/node_modules/on-finished/index.js:100:5)
    at callback (/var/www/html/nodeJs/Example-4/node_modules/express/node_modules/on-finished/node_modules/ee-first/index.js:55:10)
    at ServerResponse.onevent (/var/www/html/nodeJs/Example-4/node_modules/express/node_modules/on-finished/node_modules/ee-first/index.js:93:5)
    at ServerResponse.EventEmitter.emit (events.js:126:20)
    at ServerResponse.OutgoingMessage._finish (http.js:837:8)
    at ServerResponse.OutgoingMessage.end (http.js:822:10)
    at onend (stream.js:66:10)
    at EventEmitter.emit (events.js:126:20)

旧版本的 node(v0.10 之前)不支持 setImmediate(),因此您需要升级您的 node 副本才能使代码正常工作。

要升级节点,请参阅 this page about adding a repository (or using an installer for OS X or Windows) to keep your copy of node up to date automatically. Alternatively, you can either extract a precompiled binary tarball to a place of your choosing (adjusting your $PATH as necessary) or compile and install from source. For these options see this page