/*登录验证功能
*/
const http=require('http');
const url=require('url');
const querystring=require('querystring');
const ss=require('./06.js');
const path=require('path');
const fs=require('fs');
let readFile=(paths,res)=>{
fs.readFile(path.join(dirname,'www',paths),'utf8',(err,fileContent)=>{
if(err){ // 有错误
res.end('server Error');
}else{
res.end(fileContent);
}
})
}
http.createServer((req,res)=>{
readFile('login.html',res);
if(req.url.startsWith('/www')){
ss.staticServer(req,res,path.join(dirname));
}
// // 动态资源请求
if(req.url.startsWith('/login')){
// get请求
if(req.method=='GET'){
let param=url.parse(req.url,true).query;
if(param.username=='admin' && param.password=='123'){
res.end('get success.......');
}else{
res.end('get failure.......');
}
}
// post请求
if(req.method=='POST'){
let pdata='';
req.on('data',(chunk)=>{
pdata+=chunk;
});
req.on('end',()=>{
let obj=querystring.parse(pdata);
if(obj.username=='admin' && obj.password=='123'){
res.end('post success........');
}else{
res.end('post failure.......');
}
})
}
}
}).listen(3000,()=>{
console.log('服务器启动正常');
});
post,get参数传递与获取
最新推荐文章于 2023-01-26 11:16:02 发布