mysql -u root -p
source 你的文件路径(可以直接拖进去自动识别路径)
// 新版本接口服务 const express = require('express'); const session = require('express-session') var MySQLStore = require('express-mysql-session')(session); var options = { host: 'localhost', port: 3306, user: 'root', // password: '', database: 'cake' }; var sessionStore = new MySQLStore(options); //引入路由模块 const cors = require('cors'); const index = require('./routers/index'); //创建web服务器 var server = express(); //托管静态资源到public下; server.use(express.static('public')); //跨域请求cors server.use(cors({ origin: "*", credentials: true })); // server.writeHead(200,{"Access-Control-Allow-Credentials":true}); server.use(express.json()) server.use(express.urlencoded({ extended: false })); //session 启用会话中间件,用来保存用户登录状态以及验证码 server.use(session({ name: 'sessioqqqqqq', resave: true, saveUninitialized: true, secret: 'APP_SESSION_SECRET', store: sessionStore // 将会话存到数据库 })) server.use('/index', index); server.use((req, res, next) => { res.status(404).send('404 not found') }) // 异常处理器 server.use((req, res, next, err) => { console.log('req', req) console.log('res', res) console.log('next', next) console.log(next) console.log('err', err) // res.send(500); }) server.listen(3000); console.log('啟動啦3000')
const express = require("express"); const router = express.Router(); const pool = require("../pool"); router.get("/", (req, res) => { console.log('调用了我这个接口---------------index') var status = req.query.status; var output = { product: {} } if (status !== undefined) { var sql1 = `select * from cake_index_product where index_status=?`; pool.query(sql1, [status], (err, result) => { if (err) console.log(err); output.product = result; // console.log(output); // console.log("haha1"); res.send(output); }) } else { res.send(output); console.log(444) } }) module.exports = router;
node app.js