var http = require('http')
var mysql = require('mysql')
var fs = require('fs')
var url = require('url')
var querystring = require('querystring')
var xzh = {
username: 'xzh',
password: 'sjh'
}
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'sjh',
port: '3306',
database: 'sjh'
})
connection.connect()
function login() {
var pro = new Promise(function(reslove, reject) {
var loginSql = 'select * from users'
connection.query(loginSql, function(err, result) {
if (err) {
reject(err)
}
reslove(result)
})
}).then(function(result) {
http.createServer(function(req, res) {
parseurl = url.parse(req.url, true, true)
username = parseurl.query.username || ''
passwd = parseurl.query.password || ''
id = parseurl.query.id
var info = {
id,
username,
passwd
}
res.writeHead(200, {
'Content-Type': 'text/plain'
})
// console.log(JSON.stringify(result))
// console.log(JSON.stringify(info))
// console.log(JSON.stringify(result).indexOf(JSON.stringify(info)))
if (JSON.stringify(result).indexOf(JSON.stringify(info))<0) {
res.end('result:false')
}
res.end(JSON.stringify(info))
}).listen(8880, function() {
console.log('8880 is listening.....')
})
}).catch(function(err) {
console.error(err)
})
}
login()
接口url:localhost:8880//login?username=xzh&password=sjh
网友评论