TypeError:jwt.sign不是一个函数

我包括快递等,包括:

var expressJwt = require('express-jwt'); //https://npmjs.org/package/express-jwt var secret = 'this is the secret secret secret 12356'; var jwt = require('jsonwebtoken'); //https://npmjs.org/package/node-jsonwebtoken 

然后定义我的续集模型和结尾路线,并把它放在这里:

 app.post('/authenticate', function (req, res) { //TODO validate req.body.username and req.body.password //if is invalid, return 401 if (!(req.body.username === 'john.doe' && req.body.password === 'foobar')) { res.status(401).send('Wrong user or password'); return; } var profile = { first_name: 'John', last_name: 'Doe', email: 'john@doe.com', id: 123 }; // We are sending the profile inside the token var token = jwt.sign(profile, secret, { expiresInMinutes: 60*5 }); res.json({ token: token }); }); 

当我在表单中inputjohn.doe和foobar时,我通过控制台得知jwt.sign不是一个函数,即使在npm安装之后。

jsonwebtoken仅用于在express.js 请求上validation/解码jwts。

如果您需要签署请求,则需要使用node-jsonwebtoken:

https://github.com/auth0/node-jsonwebtoken

GH问题:

https://github.com/auth0/express-jwt/issues/48

这里有一个很好的博客文章,你正在做什么:

https://matoski.com/article/jwt-express-node-mongoose/