需要安装依赖
npm install jsonwebtoken
使用
// 生成令牌
const payload = { userId: '123456789' };
const secretKey = 'your-secret-key';
const options = { expiresIn: '1h' };
const token = jwt.sign(payload, secretKey, options);
console.log('生成的令牌:', token);
解密
const decoded = jwt.verify(token, secretKey);
console.log('解密的有效载荷:', decoded);