Problem Description:
Express JWT is not a function
Cause analysis:
The express JWT I installed is version 7.7.5, and the usage has changed.
https://www.npmjs.com/package/express-jwt
Check the official documents for the latest usage
Solution:
1. import
var { expressjwt:jwt } = require("express-jwt")
2. registration
app.use(jwt({
secret: 'shhhhhhared-secret', algorithms: ['HS256']
}).unless({ path:[/^\/api\//] }))
.unless ({specify the path without access permission with regular})
Or another method, directly add the unique protected path address as a parameter:
app.use("/api", jwt({
secret: "shhhhhhared-secret", algorithms: ["HS256"]
}));
3. user data
Once parsed successfully, the user data contained in the Token will be automatically mounted on req.auth
// This is an API interface with permissions
app.get('/admin/getinfo', function (req, res) {
// TODO_05: use req.auth to get the user information and use the data property to send the user information to the client
console.log(req.auth);
res.send({
status: 200,
message: 'User information retrieved successfully! ,
data: req.auth// the user information to be sent to the client
})
})
Read More:
- Node.js Error: Cannot find module express [How to Solve]
- TypeError: r.indexOf is not a function TypeError: r.indexOf is not a function
- [Solved] Echart Error: Typeerror: axis Getaxesonzeroof is not a function
- [Solved] Syntax Error: TypeError: this.getOptions is not a function
- [Solved] Vue uses webpack to package error: Createapp is not a function
- [Solved] Input error: this. Getoptions is not a function
- [Solved] mqtt.js Error: n.createConnection is not a function
- [Solved] Less Install Error: TypeError: this.getOptions is not a function
- [Solved] Vue3.2 Error: Object.fromEntries is not a function
- [Solved] nodejs express Error: MulterError: Unexpected field
- [Solved] Syntax Error: TypeError: this.getOptions is not a function
- [Web Browser] “Uncaught TypeError: object is not a function“
- [Vue warn]: Error in v-on handler: “TypeError: Object(…) is not a function“
- [Solved] node-xlsx Write excel Error: TypeError: n.indexOf is not a function
- [Solved] scss Error: TypeError: this.getOptions is not a function
- [Solved] express-jwt Error: Error: algorithms should be set
- [Solved] webpack Package Error: TypeError: this.getOptions is not a function style-loader
- Vue Use scss Error: this.getOptions is not a function [How to Solve]
- [Solved] Module build failed TypeError this.getOptions is not a function at Object.loader
- [Solved] Vue Error: Uncaught TypeError: Vue.createApp is not a function