loading...

1

node利用express获取数据库展示在页面demo

其他读完大概需要5分钟

  • 发布时间:2017-07-27 22:25 星期四
  • 刘伟波
  • 147
  • 更新于2017-07-27 22:25 星期四

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

用户名密码昵称
张三123456abc
mary654321mm
小强987654gg
Alice654321yyy
Lily456789fizz
fdsfads123fdsfdsa
六六六123456987二狗子
jxf123123jxf
jxf123123jxfjxf
dahuaidanxu692295hzw大坏蛋
jxf11111111111jxf111
MORE123456MORE
qweasdzxcjxfasd哈哈
jxfjxfaaaaaaaajxfjxf
xuexixuexixuexi
quefangyuan123123que
jqy12344321啊哈哈哈
jxfjxfaaaaaaaajxfjxf
jxfasdfghjklaaa
我是力量的化身11111111我是火车王
songjunqingsongjunqingsong
wuyirong2350596wwu
xieshangxieshangxieshang
啊哈哈哈哈87654321啊呵呵呵呵
刘伟波a739291780lwb
wycyiaiwei吴奕辰
123123321adf
3211234567123


你可能感兴趣的文章

    发表评论

    评论支持markdown,评论内容不能超过500字符,如果内容过多或者要及时回复,建议去 平台,一般一天之内就会回复。
    关于技术问题或者有啥不懂的都可以留言,我会定期回复答 疑,推荐最新仓库 前端知识体系, 感謝支持!