ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 1.安装npm ~~~ npm init -y npm i express body-parser cookie-parser cookie-session consolidate ejs mysql multer -D ~~~ ## 2.server.js模板 ~~~ const express = require("express"); const mysql = require("mysql"); const multer = require("multer"); const multerObj = multer({ dest: "./static/upload/" }); const bodyParser = require("body-parser"); const cookieParser = require("cookie-parser"); const cookieSession = require("cookie-session"); const consolidate = require("consolidate"); const server = express(); server.listen(8080); server.use(bodyParser.urlencoded({extended:false})); /* 1.获取请求数据 */ server.use(multerObj.any()); /* 2.cookie,session */ var keys = []; for (var i = 0; i < 10000; i++) { keys.push("keys" + Math.random()); } server.use(cookieParser()); server.use( cookieSession({ name: "sess_id", keys, maxAge: 20 * 60 * 1000 }) ); /* 3.模板 */ server.engine("html", consolidate.ejs); server.set("views", "./template"); server.set("view engine", "html"); /* 4.route */ server.use("/user",require('./route/web/index.js')()); /* 5.static */ server.use(express.static("./static/")); ~~~ ## 3.每一个router作为单位的模块,拆分route ~~~ //index.js const express = require('express'); module.exports= function(){ var router = express.Router(); router.get('/1.html',function(req,res){ res.send("1").end() }) router.get("/2.html",function(req,res){ res.send("2").end() }) return router; } //之后在入口的server.js中直接使用 ~~~