准备工作
1.配置了MySQL server
2.安装express和mysql库
登录界面(http://127.0.0.1:8080/form.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="http://127.0.0.1:8080/process_get" method="GET">
user:<input type="" name="name"><br>
pass:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
服务器
var express = require('express');
var mysql = require('mysql');
var app = express();
var response={};
var Result='';
app.use(express.static('public'));
app.get('/form.html', function (req, res) {
res.sendFile( __dirname + "/" + "form.html" );
})
app.get('/process_get', function (req, res) {
// 输出 JSON 格式
response = {
"first_name":req.query.name,
"last_name":req.query.password
};
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
port: '3306',
database: '226',
});
connection.connect();
var sql = 'SELECT * FROM user_table where username="'+response.first_name+'"&&password='+response.last_name;
//查
console.log(sql);
connection.query(sql,function (err, result) {
if(err){
console.log('[SELECT ERROR] - ',err.message);
return;
}
console.log('--------------------------SELECT----------------------------');
console.log(result);
console.log('------------------------------------------------------------\n\n');
connection.end();
if(result.length != 0){
res.end('success!')
}else{
res.end('fail...')
console.log('result的值为',result)
});
// connection.end();
})
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})