node利用express获取数据库展示在页面demo
1.app.js
const express = require('express');
const http=require('http');
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit: 10,
host: '127.0.0.1',
user: 'root',
password: '',
database: 'blog'
});
var app=express();
http.createServer(app).listen(3002,"127.0.0.1");
app.use(express.static("public"));
app.get("/", function (req,res) {
res.sendFile(__dirname+"/public/index.html")
});
app.get("/getUser", function (req,res) {
pool.getConnection(function (err, connection) {
// Use the connection
connection.query('SELECT * FROM user', function (error, results, fields) {
// And done with the connection.
res.json(results);
connection.release();
// Handle error after the release.
if (error) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});
});
2.index.js
$.ajax({
url:"/getUser",
type:"get",
data:{user:"jack",age:24},
success: function (data,xhr) {
var tbody="";
$.each(data, function (i,ele) {
tbody+=`
<tr>
<td>${ele.username}</td>
<td>${ele.password}</td>
<td>${ele.nickName}</td>
</tr>
`;
});
$("tbody").append(tbody);
}
});
3.demo
用户名 | 密码 | 昵称 |
---|---|---|
张三 | 123456 | abc |
mary | 654321 | mm |
小强 | 987654 | gg |
Alice | 654321 | yyy |
Lily | 456789 | fizz |
fdsfads | 123 | fdsfdsa |
六六六 | 123456987 | 二狗子 |
jxf | 123123 | jxf |
jxf | 123123 | jxfjxf |
dahuaidanxu | 692295hzw | 大坏蛋 |
jxf111 | 11111111 | jxf111 |
MORE | 123456 | MORE |
qweasdzxc | jxfasd | 哈哈 |
jxfjxf | aaaaaaaa | jxfjxf |
xuexi | xuexi | xuexi |
quefangyuan | 123123 | que |
jqy | 12344321 | 啊哈哈哈 |
jxfjxf | aaaaaaaa | jxfjxf |
jxf | asdfghjkl | aaa |
我是力量的化身 | 11111111 | 我是火车王 |
songjunqing | songjunqing | song |
wuyirong | 2350596w | wu |
xieshang | xieshang | xieshang |
啊哈哈哈哈 | 87654321 | 啊呵呵呵呵 |
刘伟波 | a739291780 | lwb |
wyc | yiaiwei | 吴奕辰 |
123 | 123321 | adf |
321 | 1234567 | 123 |
发表评论: