var http=require('http');
var fs=require('fs');
function read(str,res){
fs.readFile(str,function(err,data){
if(!err){
res.write(data);
res.end();
}
})
}
var server=http.createServer(function(req,res){
console.log(req.url);
if(req.url=='/'){
read('./index.html',res);
}else if(req.url=='/favicon.ico'){
read('./img/ico.png',res);
}else{
read('.'+req.url,res);
}
});
server.listen(3001,'127.0.0.1',function(err){
if(!err){
console.log('服务器成功运行在127.0.0.1:3000上')
}
})
<form action="/api/login" method="get">
用户名:<input type="text" name="user">
密码:<input type="text" name="pass">
<button type="submit">登录</button>
</form>
var http=require('http');
var fs=require('fs');
var url=require('url');
var json=[{user:'tom',pass:'123456'},{user:'kity',pass:'123'}];
function read(str,res){
fs.readFile(str,function(err,data){
if(!err){
res.write(data);
res.end();
}
})
}
var server=http.createServer(function(req,res){
var obj=url.parse(req.url,true)
var pathname=obj.pathname;
var query=obj.query;
if(pathname=='/'||pathname=='/index.html'){
read('./index.html',res);
}else if(pathname=='/api/login'){
for(var i=0;i<json.length;i++){
if(json[i].user==query.user&&json[i].pass==query.pass){
read('./index2.html',res);
return;
}
}
res.setHeader('Content-Type','text/plain;charset=utf-8');
res.write('登录失败');
res.end();
}
})
server.listen(3000,function(err){
if(!err)console.log('服务器成功运行在3000端口上');
})