无法在nodeJs

时间:2016-06-21 06:12:52

标签: node.js http-post http-get

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

我尝试成功获取GET数据但在POST数据中我收到错误 ReferenceError:未定义setImmediate 。 当我使用$ node server.js运行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)

1 个答案:

答案 0 :(得分:1)

节点的古老版本(v0.10之前版本)不支持setImmediate(),因此您需要升级节点副本才能使代码生效。

要升级节点,请参阅this page有关添加存储库(或使用OS X或Windows的安装程序)以自动更新节点副本的信息。或者,您可以将预编译的二进制tarball提取到您选择的位置(根据需要调整$PATH)或从源代码编译和安装。有关这些选项,请参阅this page