今天初试egg.js,比这葫芦(https://eggjs.org/zh-cn/intro/)画瓢的过程中遇到了2个问题,已经解决,再次做下记录,同时分享给大家,以便后来遇到问题的小伙伴能够解决!
2018-08-24 10:09:25,008 WARN 11672 [-/127.0.0.1/-/127ms POST /childController] invalid csrf token. See https://eggjs.org/zh-cn/core/security.html#安全威胁csrf的防范
2018-08-24 10:09:25,013 WARN 11672 [-/127.0.0.1/-/132ms POST /childController] nodejs.ForbiddenError: invalid csrf token
at Object.throw (E:\egg_work\egg-simple\node_modules\koa\lib\context.js:96:11)
at Object.assertCsrf (E:\egg_work\egg-simple\node_modules\egg-security\app\extend\context.js:157:17)
at csrf (E:\egg_work\egg-simple\node_modules\egg-security\lib\middlewares\csrf.js:31:9)
at dispatch (E:\egg_work\egg-simple\node_modules\koa-compose\index.js:42:32)
at E:\egg_work\egg-simple\node_modules\koa-compose\index.js:34:12
at dispatch (E:\egg_work\egg-simple\node_modules\koa-compose\index.js:42:32)
at session (E:\egg_work\egg-simple\node_modules\koa-session\index.js:41:13)
at dispatch (E:\egg_work\egg-simple\node_modules\koa-compose\index.js:42:32)
at overrideMethod (E:\egg_work\egg-simple\node_modules\koa-override\index.js:38:12)
at dispatch (E:\egg_work\egg-simple\node_modules\koa-compose\index.js:42:32)
message: "invalid csrf token"
pid: 11672
hostname: DESKTOP-872SO2H
原因:
具体看 https://eggjs.org/zh-cn/faq.html#%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BC%9A%E6%9C%89-csrf- %E6%8A%A5%E9%94%99
解决方案:找到node_modules 下的 egg-security →config→config.default.js 找到csrf这个对象下的enable改为false。
问题 2:api 上说controller支持多级目录,而我测试在controller下又创建了一个新的目录,发现同样请求不到?
原因:其实我这个算是特殊的例子。
错误信息:
E:\egg_work\egg-simple\node_modules\egg\lib\application.js:69
throw e;
^
TypeError: Cannot read property 'index' of undefined
at module.exports.app (E:\egg_work\egg-simple\app\router.js:9:63)
at AppWorkerLoader.loadFile (E:\egg_work\egg-simple\node_modules\egg-core\lib\loader\egg_loader.js:299:29)
at AppWorkerLoader.loadRouter (E:\egg_work\egg-simple\node_modules\egg-core\lib\loader\mixin\router.js:17:10)
at AppWorkerLoader.load (E:\egg_work\egg-simple\node_modules\egg\lib\loader\app_worker_loader.js:41:10)
at new Application (E:\egg_work\egg-simple\node_modules\egg\lib\application.js:65:19)
at Object. (E:\egg_work\egg-simple\node_modules\egg-cluster\lib\app_worker.js:20:13)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
[2018-08-24 10:25:21.899] [cfork:master:2244] worker:5520 disconnect (exitedAfterDisconnect: false, state: disconnected, isDead: false, worker.disableRefork: true)
[2018-08-24 10:25:21.899] [cfork:master:2244] don't fork, because worker:5520 will be kill soon
2018-08-24 10:25:21,899 INFO 2244 [master] app_worker#4:5520 disconnect, suicide: false, state: disconnected, current workers: ["4"]
[2018-08-24 10:25:21.905] [cfork:master:2244] worker:5520 exit (code: 1, exitedAfterDisconnect: false, state: dead, isDead: true, isExpected: false, worker.disableRefork: true)
2018-08-24 10:25:21,905 ERROR 2244 nodejs.AppWorkerDiedError: [master] app_worker#4:5520 died (code: 1, signal: null, suicide: false, state: dead), current workers: []
at Master.onAppExit (E:\egg_work\egg-simple\node_modules\egg-cluster\lib\master.js:388:21)
at emitOne (events.js:116:13)
at Master.emit (events.js:211:7)
at Messenger.sendToMaster (E:\egg_work\egg-simple\node_modules\egg-cluster\lib\utils\messenger.js:122:17)
at Messenger.send (E:\egg_work\egg-simple\node_modules\egg-cluster\lib\utils\messenger.js:87:12)
at EventEmitter.cluster.on (E:\egg_work\egg-simple\node_modules\egg-cluster\lib\master.js:265:22)
at emitThree (events.js:141:20)
at EventEmitter.emit (events.js:217:7)
at ChildProcess.worker.process.once (internal/cluster/master.js:189:13)
at Object.onceWrapper (events.js:317:30)
name: "AppWorkerDiedError"
pid: 2244
hostname: DESKTOP-872SO2H
TypeError: Cannot read property 'index' of undefined,为什么找不到index.js呢?
最后,也是在api中找到了答案。router.js 中配置的文件级联目录+文件,会自动转为驼峰命名方式,其他则不识别
https://eggjs.org/zh-cn/advanced/loader.html#%E6%96%87%E4%BB%B6%E5%8A%A0%E8%BD%BD%E8%A7%84%E5%88%99
解决方案:将router.js 中的目录和文件名配置改为驼峰命名法。
答案都在api中,归根到底是文档没有看仔细,警示大家,看文档一定要看仔细,否则处处是坑啊