post方式:
var express = require("express");
var query = require("querystring");
var app = express();
app.listen(8000,function () {
console.log("服务器已经启动,端口号是8000");
})
//通过post请求,服务端接收到是流数据,必须把流数据通过“+”转化成字符串
//如:reqData += data2;
//new Buffer("username=admin&password=123456")
//<Buffer 75 73 65 72 6e 61 6d 65 3d 61 64 6d 69 6e 26 70 61 73 73 77 6f 72 64 3d 31 32 33 34 35 36>
app.post("/login",function (request,response) {
response.header("Access-Control-Allow-Origin", "*");
var result = "";
request.on("data",function (datas) {
console.log(datas); //流数据
result += datas; //通过+号转化成字符串
//console.log(result);
})
request.on("end",function () {
var person = query.parse(result);
console.log(person);
if(person.username == "admin" && person.password == "123456"){
response.send("登录成功");
} else {
response.send("登录失败");
}
})
})
get方式:
var express = require("express");
var app = express();
app.listen(8000,function () {
console.log("服务器已经启动,端口号是8000!");
})
app.get("/search",function (request,response) {
//跨域请求
response.header("Access-Control-Allow-Origin", "*");
var username = request.query.username;
console.log(username);
if(username == "admin") {
response.send(true);
} else {
response.send(false);
}
})