首页 Node.js Express框架

参数说明


post(parh, callback)
path               字符串         请求路径,"/":表示根目录
callback(          回调函数      接收、响应,客户端数据
    request,      接收对象      客户端请求,http.IncomingMessageke 
    response     响应对象      服务器响应,http,ServerResponse
)

 


index.html:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf8"/>
    <title>liboke.cn</title>
  </head>
  <body>
    <form action="/formpost" method="post">
        <input type="text" name="name" value=""/>
        <input type="submit" value="提交"/>
    </form>
  </body>
</html>




var querystring = require("querystring");
var app = require("express")();
app.listen(8080, "localhost"); 

app.get("/", function(req, res){  //接收GET请求
    //sendFile()为Express封装方法,将HTML发送到客户端
    res.sendFile(__dirname + "/index.html"); 
});

app.post("/formpost", function(req, res){  //接收POST请求
    req.on("data", function(data){  //获取post数据
        var obj = querystring.parse(data.toString());  //解析数据
        res.send(obj.name);  //响应文本
    });
});