<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
用户名: <input type="text" value="" id="tt">
<span id="s"></span>
</body>
<script>
var txt = document.getElementById('tt')
txt.onblur = function(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if (xhr.responseText == 1) {
var h = '<font color = "red">不可以</font>';
document.getElementById('s').innerHTML = h;
}else{
var h = '<font color = "green">可以</font>';
document.getElementById('s').innerHTML = h;
}
}}
xhr.open('get','http://127.0.0.1:8080/getname?me=' + txt.value);
xhr.send();
// xhr.open('post','http://127.0.0.1:8080/getname');
// xhr.send('me='+txt.value);
}
</script>
</html>
//引用模块
var http = require('http');
var fs = require('fs');
var url = require('url');
//开启服务器
var server = http.createServer();
server.listen(8080,function(){
console.log('启动')
})
server.on('request',function(req,res){
var urls = url.parse(req.url, true);
if(urls.pathname == '/gets'){
res.end('get');
}else if (urls.pathname == '/posts') {
res.end('post');
}else if(urls.pathname == '/getname'){
// console.log(urls.query.me);
if(urls.query.me == 'admin'){
res.end('1');
}else{
res.end('0');
}
// var d ='';
// req.on('data', function(post_data){
// d += post_data;
// })
// req.on('end', function(){
// var obj = require('querystring').parse(d);
// if(obj.me == 'admin'){
// res.end('1')
// }else{
// res.end('0');
// }
// })
}else{
fs.readFile('.'+urls.pathname, function(err, data){
if(!err){
res.end(data);
}else{
res.end('')
}
})
}
})