~~~
npm i express mysql
~~~
~~~
const express = require("express");
const consolidate = require("consolidate");
const mysql = require("mysql");
const server = express();
server.listen(8080);
var db = mysql.createPool({
host: "localhost",
user: "root",
password: "123456",
database: "blog"
});
server.set("view engine", "html");
server.set("views", "./template");
server.set("html", consolidate.ejs);
server.get("/", function(req, res,next) {
//查询banner
db.query("SELECT * FROM banner_table", (err, data) => {
if (err) {
res.status(500).send('database erro').end()
} else {
res.render('index.ejs',{banners:data});
}
});
});
/* static数据 */
server.use(express.static("./www"));
~~~