Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript res.jwt不是一个函数-NodeJS Express_Javascript_Node.js_Express - Fatal编程技术网

Javascript res.jwt不是一个函数-NodeJS Express

Javascript res.jwt不是一个函数-NodeJS Express,javascript,node.js,express,Javascript,Node.js,Express,我一直在 res.jwt is not a function 我已经安装了jwt express,并像这样导入了它 import jwt from 'jwt-express' 这是我的auth.js index.js import express from 'express' import favicon from 'serve-favicon' import path from 'path' import bodyParser from 'body-parser' import b

我一直在

res.jwt is not a function 
我已经安装了jwt express,并像这样导入了它

import jwt from 'jwt-express'  
这是我的auth.js


index.js

import express from 'express'
import favicon from 'serve-favicon'
import path from 'path'
import bodyParser from 'body-parser'
import bluebird from 'bluebird'
import jwt from 'jwt-express'
import env from 'dotenv'

//Controllers
import fortinetController from './controllers/fortinet'
import authController from './controllers/auth.js'

//Logger
import logger from './config/logger.js'

//Constant
const router = express.Router();
const app = express();
const PORT = 3000;
const dotenv = env.config();
Promise = bluebird;

app.use(bodyParser.urlencoded({extended: true }));
app.use(bodyParser.json());
app.use(router)
app.use(express.static('public'))
app.use(favicon(path.join(__dirname,'public','favicon.ico')))
app.use(jwt.init('CARWASH', {cookies: false }));


router.get('/', (req,res) => {
    res.send('Welcome to the backend provisioning daemon to program FortiManager')
});

router.post('/login', authController.login);

//Fortinet
router.post('/fortinet/login', fortinetController.login);
router.post('/fortinet/getSessionTimeOut', fortinetController.getSessionTimeOut);
router.post('/fortinet/logout', fortinetController.logout);

//Error handling function
app.use((err,req,res,next) => {
    console.error(err.stack)
    res.status(500).send(`Red alert! Red alert!: ${err.stack}`)
    logger.error(`${req.method} ${req.url} - ${err.log || err.message}`);
});

app.listen(PORT, () => {
        console.log(`Your server is running on ${PORT}`)
    }
);
我如何调试这个


更新 我试着加上这个

console.log(jwt);
我得到

  • 您没有正确配置
    express jwt
  • 您使用的
    express jwt
    完全错误
  • 让我们浏览每一点

    我不知道为什么您认为需要调用
    jwt.init(…)
    ,而文档中只说明了这样做:
    jwt(…)
    。因此,您需要进行以下更改:

    改变

    app.use(jwt.init('CARWASH', {cookies: false }));
    

    不存在
    cookies
    选项,不确定从何处获得该选项

    现在,
    expressjwt
    将只处理jwt的验证。它不会像您在
    auth.js
    中尝试的那样为生成JWT

    为了生成JWT,您需要另一个模块:。然后,您将在
    auth.js
    中使用该模块,如下所示:

    import jwt from "jsonwebtoken";
    // ...
    
    module.export = {
        async login(req, res, next) {
            try {
                // ... auth logic omitted
    
                // Here we generate the JWT
                // Make sure the JWT secret is the SAME secret you used for express-jwt
                let authentication = jwt.sign({
                    'email': account.email_address,
                    'id': account.account_id
                }, 'CARWASH');
                res.send(authentication);
            }
            catch (error) {
                next(error);
            }
        }
    }
    
  • 您没有正确配置
    express jwt
  • 您使用的
    express jwt
    完全错误
  • 让我们浏览每一点

    我不知道为什么您认为需要调用
    jwt.init(…)
    ,而文档中只说明了这样做:
    jwt(…)
    。因此,您需要进行以下更改:

    改变

    app.use(jwt.init('CARWASH', {cookies: false }));
    

    不存在
    cookies
    选项,不确定从何处获得该选项

    现在,
    expressjwt
    将只处理jwt的验证。它不会像您在
    auth.js
    中尝试的那样为生成JWT

    为了生成JWT,您需要另一个模块:。然后,您将在
    auth.js
    中使用该模块,如下所示:

    import jwt from "jsonwebtoken";
    // ...
    
    module.export = {
        async login(req, res, next) {
            try {
                // ... auth logic omitted
    
                // Here we generate the JWT
                // Make sure the JWT secret is the SAME secret you used for express-jwt
                let authentication = jwt.sign({
                    'email': account.email_address,
                    'id': account.account_id
                }, 'CARWASH');
                res.send(authentication);
            }
            catch (error) {
                next(error);
            }
        }
    }
    

    嗯,你已经调试了什么是itI,我不知道如何调试。然后你应该问,否则每次你得到一个你不期望的结果时,你都会遇到问题。添加--检查你的npm脚本中的所有内容似乎都很好,应用程序正在运行,我安装了所有内容,并且也导入了它。我被绊倒了。你调试过了吗?我不知道怎么做。然后你应该问,否则每次你得到一个你不期望的结果时,你都会遇到问题。添加——在你的npm脚本中检查一切似乎都很好,应用程序正在运行,我安装了所有东西,并且也导入了它。我被绊住了,我应该把这条线移走吗
    从“jwt express”导入jwt
    我从这个npm包中得到了cookie:
    jwt express
    我的错误,我不知道还有另一个类似于
    jwt
    的名字。无论如何,上面的答案是针对
    express jwt
    的,它将实现您想要的。请提供我要解决的包的答案,而不是建议一个新的。那么我应该删除这一行吗
    从“jwt express”导入jwt
    我从这个npm包中得到了cookie:
    jwt express
    我的错误,我不知道还有另一个类似于
    jwt
    的名字。无论如何,上面的答案是针对
    express jwt
    的,它将实现您想要的。请提供我想要解决的包的答案,而不是建议一个新的包。
    import jwt from "jsonwebtoken";
    // ...
    
    module.export = {
        async login(req, res, next) {
            try {
                // ... auth logic omitted
    
                // Here we generate the JWT
                // Make sure the JWT secret is the SAME secret you used for express-jwt
                let authentication = jwt.sign({
                    'email': account.email_address,
                    'id': account.account_id
                }, 'CARWASH');
                res.send(authentication);
            }
            catch (error) {
                next(error);
            }
        }
    }