2017-02-07 11 views
1

Iam学習帆と最後の日、私はPassport Js認証を実装することができましたが、今日は1つのエラーが表示されます。それは私が同じ手順をとったので変だけど、今、すべてが失敗します。依存関係をインストールするのを忘れるのですか?フック( `http`)の読み込みに失敗しました! SailsJs

エラーの原因はわかりません。

Express midleware for passport 
error: A hook (`http`) failed to load! 
error: ReferenceError: express is not defined 
    at Object.module.exports.http.customMiddleware (/home/tatico/estudiomate/config/passport.js:46:12) 
    at /home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:237:33 
    at arrayEach (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:1439:13) 
    at Function.<anonymous> (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3500:13) 
    at _afterLoadingSessionHookIfApplicable (/home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:224:11) 
    at /home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:31:18 
    at /home/tatico/.npm-global/lib/node_modules/sails/lib/app/private/after.js:91:14 
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:721:13 
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:52:16 
    at async.forEachOf.async.eachOf (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:236:30) 
    at _parallel (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:712:9) 
    at Object.async.parallel (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:726:9) 
    at Sails.emitter.after (/home/tatico/.npm-global/lib/node_modules/sails/lib/app/private/after.js:89:11) 
    at _waitForSessionHookIfApplicable (/home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:30:15) 
    at Hook.initialize (/home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/http/initialize.js:42:7) 
    at Hook.wrapper [as initialize] (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3250:19) 
    at /home/tatico/.npm-global/lib/node_modules/sails/lib/hooks/index.js:88:16 
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:52:16 
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:548:17 
    at /home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:542:17 
    at _arrayEach (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:85:13) 
    at Immediate.taskComplete (/home/tatico/.npm-global/lib/node_modules/sails/node_modules/async/lib/async.js:541:13) 

これらは私のファイルは、次のとおりです: は、これは誤りである

package.json:

{ 
    "name": "estudioMate", 
    "private": true, 
    "version": "0.0.0", 
    "description": "a Sails application", 
    "keywords": [], 
    "dependencies": { 
    "bcrypt": "^1.0.2", 
    "ejs": "2.3.4", 
    "grunt": "1.0.1", 
    "grunt-contrib-clean": "1.0.0", 
    "grunt-contrib-coffee": "1.0.0", 
    "grunt-contrib-concat": "1.0.1", 
    "grunt-contrib-copy": "1.0.0", 
    "grunt-contrib-cssmin": "1.0.1", 
    "grunt-contrib-jst": "1.0.0", 
    "grunt-contrib-less": "1.3.0", 
    "grunt-contrib-uglify": "1.0.1", 
    "grunt-contrib-watch": "1.0.0", 
    "grunt-sails-linker": "~0.10.1", 
    "grunt-sync": "0.5.2", 
    "include-all": "^1.0.0", 
    "passport": "^0.3.2", 
    "passport-local": "^1.0.0", 
    "rc": "1.0.1", 
    "sails": "~0.12.11", 
    "sails-disk": "~0.10.9" 
    }, 
    "scripts": { 
    "debug": "node debug app.js", 
    "start": "node app.js" 
    }, 
    "main": "app.js", 
    "repository": { 
    "type": "git", 
    "url": "git://github.com/tatico/estudioMate.git" 
    }, 

    "author": "tatico", 
    "license": "" 
} 

のconfig/passport.js:

var passport  = require('passport'), 
    LocalStrategy = require('passport-local').Strategy 
    bcrypt  = require('bcrypt'); 


passport.use(new LocalStrategy(
    function(email, password, done) { 
    User.findOne({ email: email }, function(err, user) { 
     if (err) { return done(err); } 
     if (!user) { 
     return done(null, false, { message: 'Incorrect username.' }); 
     } 
     if (!user.validPassword(password)) { 
     return done(null, false, { message: 'Incorrect password.' }); 
     } 
     return done(null, user); 
    }); 
    } 
)); 

passport.serializeUser(function(user, done) { 
    done(null, user.id); 
}); 

passport.deserializeUser(function(id, done) { 
    User.findById(id, function(err, user) { 
    done(err, user); 
    }); 
}); 

module.exports = { 
    providers: { 
     'github': {}, 
     'facebook': {}, 
     'twitter': {} 
    }, 
    http: { 
     customMiddleware: function(app){ 
      console.log('Express midleware for passport'); 
      app.use(express.static('public')); 
      app.use(express.cookieParser()); 
      app.use(express.bodyParser()); 
      app.use(express.session({ secret: 'keyboard cat' })); 
      app.use(passport.initialize()); 
      app.use(passport.session()); 
      app.use(app.router); 
      app.use(function(req,res,next){ 
       // Set the loggedUser in locals 
       // to get it from the view 
       res.locals.loggedUser = req.user; 
       next(); 
      }); 
     } 
    } 
}; 

モデル/ユーザー。 js:

module.exports = { 

    attributes: { 
     email: { 
      type: 'email', 
      unique: true 
     }, 

     admin: { 
      type: 'boolean', 
      defaultsTo: false 
     }, 

     password: { 
      type: 'string', 
      required: true 
     }, 

     toJSON: function() { 
      var obj = this.toObject(); 
      delete obj.password; 
      return obj; 
     }, 

     // Check a password with the saved one. 
     validPassword: function(password, callback) { 
      var obj = this.toObject(); 
      // If there are a callback, compare async. 
      if (callback) { 
      //callback (err, res) 
      return bcrypt.compare(password, obj.password, callback); 
      } 
      // Otherwise, compare sync. 
      return bcrypt.compareSync(password, obj.password); 
     }, 

     hasPassword: function(){ 
      return !!this.password; 
     } 

    }, 

    // Lifecycle Callbacks 
    beforeCreate: function(values, next) { 
     hashPassword(values, next); 
    }, 

    beforeValidation: function(values, next) { 
     if(values.new_password && values.confirm_password) { 
      var newPasswordMatch = values.new_password === values.confirm_password; 
      if(newPasswordMatch) { 
       User.findOne(values.id).done(function(err, user) { 
        if (err) return next(err); 
        if(!values.password || user.validPassword(values.password)){ 
         // If old password is valid. 
         // Ovewrite password with the new password. 
         values.password = values.new_password; 
         // delete new password and confirmation. 
         delete values.confirm_password; 
         delete values.new_password; 
         // Hash the password. 
         hashPassword(values, next); 
        } 
       }); 
      } 
     } else if (values.id) { 
      // If we are updating the data but the password is not submited. 
      User.findOne(values.id).done(function(err, user) { 
       if (err) { 
        return next(err); 
       } else { 
        // Take the same password user had. 
        values.password = user.password; 
        next(); 
       } 
      }); 
     } else { 
      next(); 
     } 
    }, 

    beforeUpdate: function(values, next) { 
     if(values.password) hashPassword(values, next); 
     else next(); 
    } 
}; 

var bcrypt = require('bcrypt'); 

function hashPassword(values, next) { 
    bcrypt.hash(values.password, 10, function(err, hash) { 
     if (err) return next(err); 

     values.password = hash; 
     next(); 
    }); 
}; 
+1

の上部にvar express = require('express');を追加するvar Expressは=( '表現')が必要です '追加;' 'passport.js'の上部にあるため – Sangharsh

+0

おかげで私に答えます。 エラーは表示されませんでしたが、今度は次の行に表示されます: エラー:フック( 'orm')の読み込みに失敗しました! –

+1

このエラーが解決した場合はこれを受け入れて、ログに新しい質問を投稿してください – Sangharsh

答えて

0

はpassport.js

+1

スタックオーバーフローへようこそ:-) [回答]をご覧ください。コードが問題を解決する理由を説明する必要があります。 コードのみの回答は、コミュニティにとって有用ではありません。 – JimHawkins

関連する問題