module.exports导出默认值:如何以正确的方式重写?

时间:2018-12-04 15:51:14

标签: node.js ecmascript-6

我正在开发某些节点应用程序,希望所有代码都符合ES6 +标准。

因此,我试图摆脱module.exports并将其替换为export default。

如何重写以下代码以使其正常工作?

import { Strategy, ExtractJwt } from 'passport-jwt';
import mongoose from 'mongoose';
import { secretOrKey } from './keys';

const User = mongoose.model('users');
const opts = {};

opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = secretOrKey;

module.exports = passport => {
  passport.use(
    new Strategy(opts, (jwt_payload, done) => {
      User.findById(jwt_payload.id)
        .then(user => {
          if (user) {
            return done(null, user);
          }
          return done(null, false);
        })
        .catch(err => console.log(err));
    })
  );
};

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找类似的东西?

import { Strategy, ExtractJwt } from 'passport-jwt';
import mongoose from 'mongoose';
import { secretOrKey } from './keys';

const User = mongoose.model('users');
const opts = {};

opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = secretOrKey;

const someFunc = passport => {
  passport.use(
    new Strategy(opts, (jwt_payload, done) => {
      User.findById(jwt_payload.id)
        .then(user => {
          if (user) {
            return done(null, user);
          }
          return done(null, false);
        })
        .catch(err => console.log(err));
    })
  );
};

export default someFunc;