egg-cors
用来允许前端进行跨域请求的一个插件
- npm i egg-cors --save
- 在plugin.js中设置开启cors
exports.cors = {
enable: true,
package: 'egg-cors',
};
- 在config/config.default.js的module.exports里面添加
config.security = {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: ["*"]
};
config.cors = {
origin: "*", // 匹配规则 域名+端口 *则为全匹配
allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH"
};
egg-mysql
连接mysql插件
- npm i --save egg-mysql
- 开启插件 //config/plugin.js
mysql: {
enable: true,
package: "egg-mysql"
}
- 配置MYSQL config // config/config.default.js
config.mysql = {
client: {
// host
host: "127.0.0.1",
// 端口号
port: "3306",
// 用户名
user: "root",
// 密码
password: "123456",
// 数据库名
database: "myBlog"
},
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false
};
我在配置好config运行之后报了这个错误
解决办法
我的数据库是装在阿里云上面的,所以先连接服务器,在执行以下步骤:
- 登录数据库
mysql -uroot -p - 输入root的密码
- 更改加密方式
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; - 刷新:
FLUSH PRIVILEGES;