企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
>演示代码地址 [pro_init_banner](https://gitee.com/chengbenchao/node/tree/master/04mysql/01pro_init_banner) ~~~ //server.js const express = require("express"); const bodyParser = require("body-parser"); const multer = require("multer"); const cookieParser = require("cookie-parser"); const cookieSession = require("cookie-session"); const consolidate = require("consolidate"); const mysql = require("mysql"); const server = express(); server.listen(8080); var arr = []; for (var i = 0; i < 1000; i++) { arr.push("keys" + Math.random()); } var db = mysql.createPool({ host: "localhost", user: "root", password: "123456", database: "blog" }); db.query(); /* 1.解析cookie */ server.use(cookieParser("qwert")); /* 2.使用session */ server.use(cookieSession({ name: "zen_id", keys: arr })); /* 3.post数据 */ server.use(bodyParser.urlencoded({ extended: false })); server.use(multer({ dest: "./www/assets/" }).any()); /* 4.用户请求 */ server.post("/", function(req, res) { var newName = req.files[0].path + pathLib.extname(req.files[0].originalname); fs.rename(req.files[0].path, newName, function(err) { if (err) { res.send("上传失败"); } else { res.send("成功"); } }); /* 1.获取原始的扩展名 2.重命名 */ }); server.set("view engine", "html"); server.set("views", "./template"); server.set("html", consolidate.ejs); server.get("/", function(req, res) { //查询banner db.query("SELECT * FROM banner_table", (err, data) => { if (err) { res.status(500).send('database erro').end() } else { console.log(data) res.render("index.ejs", { banners:data}); } }); }); /* static数据 */ server.use(express.static("./www")); ~~~ ~~~ //index.ejs <% for(var i=0;i<banners.length;i++){ %> <img src="<%= banners[i].src %>" > <% } %> ~~~