用nodejs做一個(gè)登陸網(wǎng)站功能,集成后臺(tái)邏輯蜘犁,實(shí)現(xiàn)頁(yè)面提供乏矾。
const http = require('http')const url = require('url')
const mysql = require('mysql')
const dbServer = mysql.createConnection({
host:'localhost',
database:'sjh',
port:'3306',
user:'root',
password:'sjh'
})
dbServer.connect()
var login = (id,username,passwd)=>{
return new Promise((reslove,reject)=>{
? const sql = 'select * from users where username="' + username + '" and passwd="' + passwd + '"'
dbServer.query(sql,(err,result)=>{
if (err) {
reject(err)
console.error(err)
return
}else{
reslove(result)
}
})
})
}
http.createServer((req,res)=>{
const parseURL = url.parse(req.url,true,true)
const username = parseURL.query.username
const pathname = parseURL.pathname
if (pathname==='/login') {
const passwd = parseURL.query.passwd
const id = parseURL.query.id
const info = {
id,
username,
passwd
}
login(id,username,passwd).then((result)=>{
console.log(JSON.stringify(result))
console.log(JSON.stringify(info))
if (JSON.stringify(result).indexOf(JSON.stringify(info))>0) {
res.end(''+JSON.stringify(result[0]))
}else{
res.end('this user is not exist!')
}
})
}else{
res.end('<h1>404!not found!</h1>')
}
}).listen(8800,()=>{
console.log('8800 is working now....')
})
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
? ? <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<form name="loginform" method="post">
<input id="id" type="text">
<input id="usr" type="text" >
<input id='pwd' type="password">
<input type="button" value="登錄">
</form>
<script>
$('form').click(login)
function login(res){
var id = $('#id').val()
var username = $('#usr').val()
var passwd = $('#pwd').val()
$.post('http://192.168.119.150:8880/login?id='+id+'&username='+username+'&password='+passwd)
}
</script>
</body>
</html>I?