无法在nodeJs中接收POST数据

凯塔夫

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

我尝试并成功获取GET数据,但是在POST数据中出现错误ReferenceError:setImmediate未定义当我运行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)
mscdex

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

要升级节点,请参阅此页面有关添加存储库(或使用OS X或Windows的安装程序)以使节点副本自动保持最新。或者,您可以将预编译的二进制tarball解压缩到您选择的位置($PATH根据需要进行调整),也可以从源代码进行编译和安装。有关这些选项,请参见此页面

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章