express搭建
Express - 基于 Node.js 平台的 web 应用开发框架 - Express 中文文档 | Express 中文网 (expressjs.com.cn)
Express 应用程序生成器 - Express 中文文档 | Express 中文网 (expressjs.com.cn)
npm install cors --save跨域
- app
const cors = require('cors')
app.use(cors())
npm install
npx express-generator
npm i mysql -S
启动
npm start
- db文件夹
- index.js
const mysql = require('mysql')
const dbOption = require('./config')
//创建连接池
const pool = mysql.createPool(dbOption)
// let sql = 'insert into user (name,city,sex) values (?,?,?)'
function query (sql,params) {
return new Promise((resolve, reject) => {
//获取连接
pool.getConnection((err, conn) => {
if (err){
reject(err)
return
}
//执行sql语句
conn.query(sql, params, (err, result) => {
conn.release()
if (err) {
reject(err)
return
}
resolve(result)
})
})
})
}
module.exports = query
- config
let dbObption
dbObption = {
connectionLimit: 10,
host: 'localhost',
user: 'root',
password: 'zjxzhj123',
port: '3306',
database: 'user_text'
}
module.exports = dbObption
本文来自博客园,作者:zjxgdq,转载请注明原文链接:https://www.cnblogs.com/zjxzhj/p/14887132.html