const fs = require(‘fs’)
const path = require(‘path’)
const { mysql: config } = require(’…/config’)
console.log(‘开始初始化数据库…’)
// 初始化 SQL 文件路径
const INIT_DB_FILE = path.join(__dirname, ‘…/tools/cAuth.sql’)
const DB = require(‘knex’)({
client: ‘mysql’,
connection: {
host: config.host,
port: config.port,
user: config.user,
password: config.pass,
database: config.db,
charset: config.char,
multipleStatements: true
}
})
console.log(`准备读取 SQL 文件:${INIT_DB_FILE}`)
// 读取 .sql 文件内容
const content = fs.readFileSync(INIT_DB_FILE, ‘utf8’)
console.log(‘开始执行 SQL 文件…’)
console.log(content)
// 执行 .sql 文件内容
DB.raw(content).then(res => {
process.exit(0)
console.log(‘数据库初始化成功!’)
}, err => {
console.log(err)
throw new Error(err)
})
console.log(’*******************’)
运行以上代码服务器就崩了
16:18:49 PM2 | pid=8056 msg=failed to kill - retrying in 100ms
16:18:49 0|session | C:\Users\刘兵\Desktop\wafer2-startup\server\middlewares\response.js:6
16:18:49 0|session | module.exports = async function (ctx, next) {
16:18:49 0|session | ^^^^^^^^
16:18:49 0|session | SyntaxError: Unexpected token function
16:18:49 0|session | at createScript (vm.js:56:10)
16:18:49 0|session | at Object.runInThisContext (vm.js:97:10)
16:18:49 0|session | at Module._compile (module.js:542:28)
16:18:49 0|session | at Object.Module._extensions…js (module.js:579:10)
16:18:49 0|session | at Module.load (module.js:487:32)
16:18:49 0|session | at tryModuleLoad (module.js:446:12)
16:18:49 0|session | at Function.Module._load (module.js:438:3)
16:18:49 0|session | at Module.require (module.js:497:17)
16:18:49 0|session | at require (internal/module.js:20:19)