eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.9k stars 1.82k forks source link

egg-ts start报错并弹出node命令框 #3087

Closed DuJiming closed 6 years ago

DuJiming commented 6 years ago

export default (appInfo: EggAppInfo) => { const config = {} as PowerPartial;

config.keys = appInfo.name + 'key9527';

config.middleware = ['tokenAuth', 'errorHandler'];

config.security = { xframe: { enable: false, }, csrf: { enable: false, }, domainWhiteList: [ 'http://127.0.0.1:8080', 'http://localhost:8080'], }; config.cors = { allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS', credentials: true, };

config.jwt = { enable: true, secret: '123456@data.secret', expiresIn: 24 60 60, // 有效期24小时 algorithm: 'HS512', // hmac摘要算法 };

const bizConfig = { sourceUrl: https://github.com/eggjs/examples/tree/master/${appInfo.name}, // 自定义拦截器配置 tokenAuth: { name: 'tokenAuth', // 放过这个路径 ignore: ['/api/login'], enable: true, } };

return { ...config, ...bizConfig, }; };

### 说我这个文件有问题,不知道是哪里出错了
`tokenAuth.ts`:

import { Context, /Application, /EggAppConfig } from 'egg'; import * as jwt from 'jsonwebtoken'; import fs = require('fs'); import util = require('util'); import path = require('path'); const verify = util.promisify(jwt.verify); const publicKey = fs.readFileSync(path.join(__dirname, '../../config/publicKey.pub'));

export default function tokenAuth(options: EggAppConfig['tokenAuth']/, app: Application/): any { return async (ctx: Context, next: () => Promise) => { const token = ctx.header.cookie; if (ctx.url.split('/')[1]) { if (token) { // 解密payload,获取用户名和ID const payload = await verify(token.split('=')[1], publicKey); const res: any = await ctx.service.user.getUser({username: payload.username}); if (res.length) { ctx.user = { username: payload.username, id: payload.userID, }; } else { delete ctx.header.cookie; ctx.throw(401, { message: 'token无效' }); } } else { if (ctx.url.split('/')[2] !== 'login') { ctx.throw(401, { message: '无token' }); } } } console.info('拦截器名称:', options.name); await next(); }; }

`common-error.log`:

2018-10-12 10:55:17,968 ERROR 10464 [-/127.0.0.1/-/21ms GET /api/authTest] nodejs.Error: Please set config.keys first at Application.get keys [as keys] (D:\Webstorm workspace\egg_token\server\node_modules\egg\lib\application.js:321:15) at Object.get cookies [as cookies] (D:\Webstorm workspace\egg_token\server\node_modules\egg\app\extend\context.js:22:66) at Object.get [egg-security#CSRF_SECRET] (D:\Webstorm workspace\egg_token\server\node_modules\egg-security\app\extend\context.js:86:33) at Object.ensureCsrfSecret (D:\Webstorm workspace\egg_token\server\node_modules\egg-security\app\extend\context.js:97:13) at csrf (D:\Webstorm workspace\egg_token\server\node_modules\egg-security\lib\middlewares\csrf.js:14:9) at dispatch (D:\Webstorm workspace\egg_token\server\node_modules\koa-compose\index.js:42:32) at D:\Webstorm workspace\egg_token\server\node_modules\koa-compose\index.js:34:12 at dispatch (D:\Webstorm workspace\egg_token\server\node_modules\koa-compose\index.js:42:32) at session (D:\Webstorm workspace\egg_token\server\node_modules\koa-session\index.js:41:13) at dispatch (D:\Webstorm workspace\egg_token\server\node_modules\koa-compose\index.js:42:32)

pid: 10464 hostname: DJM // 下面还有多个

`master-stderr.log`: 

[2018-10-12 11:58:34.302] [cfork:master:6316] new worker:4360 fork (state: none) [2018-10-12 11:58:34.303] [cfork:master:6316] worker:2640 exit (code: 3221225786, exitedAfterDisconnect: false, state: dead, isDead: true, isExpected: true, worker.disableRefork: false) 2018-10-12 11:58:34,324 ERROR 6316 nodejs.AppWorkerDiedError: [master] app_worker#4:2640 died (code: 3221225786, signal: null, suicide: false, state: dead), current workers: ["1","2","3","5"] at Master.onAppExit (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:400:21) at Master.emit (events.js:182:13) at Messenger.sendToMaster (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:133:17) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:98:12) at EventEmitter.cluster.on (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:268:22) at EventEmitter.emit (events.js:187:15) at ChildProcess.worker.process.once (internal/cluster/master.js:194:13) at Object.onceWrapper (events.js:273:13) at ChildProcess.emit (events.js:182:13) at Process.ChildProcess._handle.onexit (internal/child_process.js:237:12) name: "AppWorkerDiedError" pid: 6316 hostname: DJM ... ... ... [2018-10-12 11:58:58.241] [cfork:master:6316] master uncaughtException: Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed at process.target.send (internal/child_process.js:627:16) at Messenger.sendToParent (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:141:29) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:107:12) at Master.onAgentExit (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:344:22) at Master.emit (events.js:182:13) at Messenger.sendToMaster (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:133:17) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:98:12) at ChildProcess.agentWorker.once (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:220:22) at Object.onceWrapper (events.js:273:13) at ChildProcess.emit (events.js:182:13) Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed at process.target.send (internal/child_process.js:627:16) at Messenger.sendToParent (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:141:29) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:107:12) at Master.onAgentExit (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:344:22) at Master.emit (events.js:182:13) at Messenger.sendToMaster (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:133:17) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:98:12) at ChildProcess.agentWorker.once (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:220:22) at Object.onceWrapper (events.js:273:13) at ChildProcess.emit (events.js:182:13) (total 12 disconnect, 0 unexpected exit) 2018-10-12 11:59:01,608 ERROR 6316 nodejs.AgentWorkerDiedError: [master] agent_worker#8:8676 died (code: 3221225786, signal: null) at Master.onAgentExit (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:330:17) at Master.emit (events.js:182:13) at Messenger.sendToMaster (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:133:17) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:98:12) at ChildProcess.agentWorker.once (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:220:22) at Object.onceWrapper (events.js:273:13) at ChildProcess.emit (events.js:182:13) at Process.ChildProcess._handle.onexit (internal/child_process.js:237:12) name: "AgentWorkerDiedError" pid: 6316 hostname: DJM

[2018-10-12 11:59:01.609] [cfork:master:6316] master uncaughtException: Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed at process.target.send (internal/child_process.js:627:16) at Messenger.sendToParent (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:141:29) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:107:12) at Master.onAgentExit (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:344:22) at Master.emit (events.js:182:13) at Messenger.sendToMaster (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:133:17) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:98:12) at ChildProcess.agentWorker.once (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:220:22) at Object.onceWrapper (events.js:273:13) at ChildProcess.emit (events.js:182:13) Error [ERR_IPC_CHANNEL_CLOSED]: Channel closed at process.target.send (internal/child_process.js:627:16) at Messenger.sendToParent (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:141:29) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:107:12) at Master.onAgentExit (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:344:22) at Master.emit (events.js:182:13) at Messenger.sendToMaster (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:133:17) at Messenger.send (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\messenger.js:98:12) at ChildProcess.agentWorker.once (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:220:22) at Object.onceWrapper (events.js:273:13) at ChildProcess.emit (events.js:182:13) (total 12 disconnect, 0 unexpected exit) 2018-10-12 11:59:08,692 ERROR 6316 nodejs.ClusterWorkerExceptionError: [master] 1 agent and 0 worker(s) alive, exit to avoid unknown state at Manager.Master.workerManager.on (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\master.js:137:19) at Manager.emit (events.js:182:13) at Timeout.timer.setInterval [as _onTimeout] (D:\Webstorm workspace\egg_token\server\node_modules\egg-cluster\lib\utils\manager.js:67:14) at ontimeout (timers.js:427:11) at tryOnTimeout (timers.js:289:5) at listOnTimeout (timers.js:252:5) at Timer.processTimers (timers.js:212:10) name: "ClusterWorkerExceptionError" count: {"agent":1,"worker":0} pid: 6316 hostname: DJM

2018-10-12 11:59:08,692 ERROR 6316 [master] exit with code:1

atian25 commented 6 years ago

再去看下 TS EGG 的文档吧,里面提到了,上线的话,是要 tsc 编译为 js,而不是 ts 文件。

DuJiming commented 6 years ago

可是我一直tsc失败。。。崩溃了。。。

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! server@1.0.0 lint: `tslint --project . -c tslint.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the server@1.0.0 lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\DJM\AppData\Roaming\npm-cache\_logs\2018-10-12T03_53_31_133Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! server@1.0.0 ci: `npm run lint && npm run cov && npm run tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the server@1.0.0 ci script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\DJM\AppData\Roaming\npm-cache\_logs\2018-10-12T03_53_31_172Z-debug.log
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'ci' ]
2 info using npm@5.6.0
3 info using node@v10.2.0
4 verbose run-script [ 'preci', 'ci', 'postci' ]
5 info lifecycle server@1.0.0~preci: server@1.0.0
6 info lifecycle server@1.0.0~ci: server@1.0.0
7 verbose lifecycle server@1.0.0~ci: unsafe-perm in lifecycle true
8 verbose lifecycle server@1.0.0~ci: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;D:\Webstorm workspace\egg_token\server\node_modules\.bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;%JAVA_HOME%;D:\Java\jre1.8.0_101\bin;D:\Java\jre6\bin;D:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;C:\WINDOWS\System32\OpenSSH\;C:\Users\DJM\AppData\Local\Microsoft\WindowsApps;C:\Users\DJM\AppData\Roaming\npm;
9 verbose lifecycle server@1.0.0~ci: CWD: D:\Webstorm workspace\egg_token\server
10 silly lifecycle server@1.0.0~ci: Args: [ '/d /s /c', 'npm run lint && npm run cov && npm run tsc' ]
11 silly lifecycle server@1.0.0~ci: Returned: code: 2  signal: null
12 info lifecycle server@1.0.0~ci: Failed to exec ci script
13 verbose stack Error: server@1.0.0 ci: `npm run lint && npm run cov && npm run tsc`
13 verbose stack Exit status 2
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:285:16)
13 verbose stack     at EventEmitter.emit (events.js:182:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:182:13)
13 verbose stack     at maybeClose (internal/child_process.js:961:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:5)
14 verbose pkgid server@1.0.0
15 verbose cwd D:\Webstorm workspace\egg_token\server
16 verbose Windows_NT 10.0.17134
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "ci"
18 verbose node v10.2.0
19 verbose npm  v5.6.0
20 error code ELIFECYCLE
21 error errno 2
22 error server@1.0.0 ci: `npm run lint && npm run cov && npm run tsc`
22 error Exit status 2
23 error Failed at the server@1.0.0 ci script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 2, true ]

再去看下 TS EGG 的文档吧,里面提到了,上线的话,是要 tsc 编译为 js,而不是 ts 文件。

atian25 commented 6 years ago

npm ERR! server@1.0.0 lint: tslint --project . -c tslint.json

你的代码 lint 挂了吧,错误提示写了的,细心看看。

DuJiming commented 6 years ago

错误的ERR!server@1.0.0功能区:tslint --project . -c tslint.json

你的代码 lint 挂了吧,错误提示写了的,细心看看。

还是不太懂,不过谢谢你了,刚接触ts,我自己再学一学

atian25 commented 6 years ago

你那个失败是跑 npm run ci 了吧,里面有一个 tslint 检测代码风格,挂了。

另外,是 npm run tsc

DuJiming commented 6 years ago

是的,跑了个ci出错了,跑tsc没问题,但是start不起来