Node.js 无法在nodeJs中接收POST数据
我是nodeJs的初学者。我只想在nodeJs中接收GET和POST数据 我尝试并成功获取数据,但在POST数据中,我获取了错误引用错误:未定义setImmediate。 当我使用Node.js 无法在nodeJs中接收POST数据,node.js,http-post,http-get,Node.js,Http Post,Http Get,我是nodeJs的初学者。我只想在nodeJs中接收GET和POST数据 我尝试并成功获取数据,但在POST数据中,我获取了错误引用错误:未定义setImmediate。 当我使用$node server.js运行server.js时,它将在3000端口监听,并重定向到index.html页面并通过一个错误 我用谷歌搜索了一下,但没有找到解决我错误的方法 我附上密码 server.js var express = require("express"); var bo
$node server.js
运行server.js时,它将在3000
端口监听,并重定向到index.html
页面并通过一个错误
我用谷歌搜索了一下,但没有找到解决我错误的方法
我附上密码
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>
node的早期版本(v0.10之前)不支持setImmediate()
,因此需要升级node副本才能使代码正常工作
要升级节点,请参阅关于添加存储库(或使用OS X或Windows安装程序)以自动更新节点副本。或者,您可以将预编译的二进制tarball提取到您选择的位置(根据需要调整
$PATH
),或者从源代码处编译和安装。有关这些选项,请参见。您必须使用旧版本的node(v0.10之前)。您可以在shell提示符下使用节点-v来验证这一点。@mscdex我的机器有节点v0.8.15。。。如何升级itI?我有节点0.8.15版本,我想升级它。我使用这个命令'sudonpm installnpm-g',但是我无法升级。
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)