egg 构建一个MVC项目环境模版
1.配置数据库 mongodb
// config.default.ts
config.mongoose = {
client: {
url: 'mongodb://127.0.0.1/CloudDisk',
options: {},
// mongoose global plugins, expected a function or an array of function and options
},
};
2.关闭POST csrf校验
// config.default.ts
config.security = {
csrf: {
enable: false,
},
};
3.配置json错误提示
// config.default.ts
// 配置错误
config.onerror = {
accepts(_ctx: Context) {
// if (ctx.get('x-requested-with') === 'XMLHttpRequest') return 'json';
return 'json';
},
json(err: BCError, ctx: Context) {
this.logger.error(err);
ctx.body = {
message: err.message,
code: err.code || ctx.status || 500,
};
},
};
4.配置redis
// config.default.ts
// 配置redis
config.redis = {
client: {
port: 6379, // Redis port
host: '127.0.0.1', // Redis host
password: '',
db: 0,
},
};
5.配置统一的自定义参数校验错误提示
// 个人写法 只是一种思路 代码不放
parameter(rules: any, data?: any) {
try {
this.self.validate(rules, data);
} catch (error) {
let code = 11000;
const _error = error.errors[0] || error;
if (_error.field) {
code = rules[_error.field].code;
}
throw new BCError({ code });
}
},
6.待更新。。。