课程名称:Node.js+Koa2+MySQL打造前后端分离精品项目《旧岛》
课程章节:第5章 LinValidator校验器与Sequelize Orm生成MySQL数据表
视频:5-3 配置文件与在终端显示异常
课程讲师: 七月
课程内容:
在PositiveIntergerValidator中,如果没有supre(),则不符合javascript语法,当调用this时,之前必须要调用supre()。 这时出错但没有报错异常信息。最好的方法是看到具体的异常信息。
const { HttpException } = require("../core/http-exception")
const catchErrof = async (ctx, next) => {
try {
await next()
} catch (error) {
throw error //注意这里直接抛出异常
if (error instanceof HttpException) {
ctx.body = {
msg: error.msg,
error_code: error.errorCode,
request: `${ctx.method} ${ctx.path}`,
}
ctx.status = error.code
}
else {
//处理未知异常
ctx.body = {
msg: '我们遇到了一个错误(未知异常) ~~~',
error_code: 999,
request: `${ctx.method} ${ctx.path}`
}
ctx.status = 500
}
}
}
module.exports = catchErrofconst { LinValidator, Rule } = require('../../core/lin-validator')
class PositiveIntegerValidator extends LinValidator {
constructor() {
//super() //这里注释掉super()
this.id = [
new Rule('isInt', '需要是正整数', {min:1})
]
}
}
module.exports = {
PositiveIntegerValidator
}使用Postman调用接口时,在终端窗口会直接打印出来异常信息。 ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor at new PositiveIntegerValidator (D:\Koa2\island\app\validator\validator.js:6:3) at router.post (D:\Koa2\island\app\api\v1\classic.js:13:18) at dispatch (D:\Koa2\island\node_modules\koa-compose\index.js:42:32) at D:\Koa2\island\node_modules\koa-router\lib\router.js:425:16 at dispatch (D:\Koa2\island\node_modules\koa-compose\index.js:42:32) at D:\Koa2\island\node_modules\koa-compose\index.js:34:12 at dispatch (D:\Koa2\island\node_modules\koa-router\lib\router.js:430:31) at dispatch (D:\Koa2\island\node_modules\koa-compose\index.js:42:32) at dispatch (D:\Koa2\island\node_modules\koa-router\lib\router.js:399:32) at dispatch (D:\Koa2\island\node_modules\koa-compose\index.js:42:32)
开发环境有必要看到异常信息,生成环境没有必要看到异常信息。用配置文件可以解决。
//一个配置文件的例子
module.exports = {
environment: 'dev' //dev开发环境。 prod生成环境
}//改动后的代码
const requireDirectory = require("require-directory");
const Router = require('koa-router')
class InitManager {
//入口方法
static initCore(app) {
InitManager.app = app
InitManager.initLoadRouters()
InitManager.loadHttpException()
InitManager.loadConfig()
}
//注意这里的改动。
static loadConfig(path = '') {
const configPath = path || process.cwd() + '/config/config.js'
const config = require(configPath)
global.config = config
}
static initLoadRouters() {
const apiDirectory = `${process.cwd()}/app/api`
requireDirectory(module, apiDirectory, {
visit: whenLoadModule
})
function whenLoadModule(obj) {
if (obj instanceof Router) {
InitManager.app.use(obj.routes())
}
}
}
static loadHttpException() {
const errors = require('./http-exception')
global.errs = errors
}
}
module.exports = InitManagerconst { HttpException } = require("../core/http-exception")
const catchErrof = async (ctx, next) => {
try {
await next()
} catch (error) {
//注意这里的改动。
if (global.config.environment === 'dev') {
throw error
}
if (error instanceof HttpException) {
ctx.body = {
msg: error.msg,
error_code: error.errorCode,
request: `${ctx.method} ${ctx.path}`,
}
ctx.status = error.code
}
else {
//处理未知异常
ctx.body = {
msg: '我们遇到了一个错误(未知异常) ~~~',
error_code: 999,
request: `${ctx.method} ${ctx.path}`
}
ctx.status = 500
}
}
}
module.exports = catchErrof课程收获:
这节课讲了使用配置文件来区分当前是开发环境还是线上生成环境的这种方法。 在ThinkPHP中也有类似的设计,env文件是否存在或者env文件中的标记,就能区分当前是开发环境还是生产环境。看来不同的语言确实有想通的地方。
七月老师非常注重在讲编程知识的同时,讲编程思维,讲知识和知识之间的关系。编程是实践性非常强的工作,学习知识最好的方法是放到项目中。做项目的目的不是做项目,最终要做出来自己的项目,业务承载的是编程知识。明天继续刷后边的课程。

随时随地看视频