cnpm i express express-static cookie-parser cookie-session body-parser multer mysql ejs consolidate --save
//建立一個mysql的連接池
const db = mysql.createPool({
host:'localhost',
user:'root',
password:'root',
database:'jy1608_blog',
port:8889
})
const mysql = require('mysql');
//建立一個mysql的連接池
const db = mysql.createPool({
host:'localhost',
user:'root',
password:'root',
database:'jy1608_blog',
port:8889
})
//查詢方法
let query = function(sql){
return new Promise((resolve,reject)=>{
db.query(sql,(err,data)=>{
if(err){
reject(err);
}else{
resolve(data);
}
});
});
}
module.exports = {
query:query
}
Promise.all([Db.query("SELECT * FROM `blog_banner`;"),Db.query("SELECT title,avatar,content FROM `blog_article`;")]).then((data)=>{
let sendData = {
banners:data[0],
articles:data[1]
}
res.render('index.ejs',sendData);
}).catch((err)=>{
res.status(500).send('數據庫錯誤').end();
})