express-jwt algorithms should be set问题解决

问题

express中使用express-jwt却不能运行, 出现如下报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Debugger listening on ws://127.0.0.1:41149/3e64451b-210f-40d5-b0fa-c71b0caf4a78
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

> woc-api@0.0.0 start /home/edk24/Desktop/code/woc-api
> node ./bin/www

Debugger listening on ws://127.0.0.1:38411/ed607848-ff76-4e67-96d2-f4fce9e93a93
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
/home/edk24/Desktop/code/woc-api/node_modules/express-jwt/lib/index.js:22
if (!options.algorithms) throw new Error('algorithms should be set');
^

Error: algorithms should be set
at module.exports (/home/edk24/Desktop/code/woc-api/node_modules/express-jwt/lib/index.js:22:34)
at Object.<anonymous> (/home/edk24/Desktop/code/woc-api/app.js:19:3)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/home/edk24/Desktop/code/woc-api/bin/www:7:11)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! woc-api@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the woc-api@0.0.0 start 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! /home/edk24/.npm/_logs/2020-07-31T09_18_16_982Z-debug.log
Waiting for the debugger to disconnect...

从错误信息中得到一个异常错误algorithms should be set,意思告诉我们没有设置算法。

但这是从官方示例中copy的啊, 还能有错?

结果是确实要设置一个算法,HS256这种, 可能是官方示例没有更新的原因, 导致初次使用的我栽到这个坑里了

解决办法

中间件配置处设置算法, 如(HMAC SHA256)HS256

1
2
3
app.use(
jwt({ secret: "woc123", algorithms: ["HS256"] }).unless({ path: ["/login"] })
);

express-jwt algorithms should be set问题解决
http://edk24.com/2020/48f39db1.html
作者
余小波
发布于
2020年7月31日
许可协议