現在、オブジェクト指向の Node.js サーバーでミドルウェア「パスポート」を使用しようとしています。サーバーを再起動すると、すべて正常に動作します。認証が不要なルートにアクセスできます。しかし、認証を使用してルートにアクセスしようとすると、常に応答 401 (認証なし) が返されます。これは問題ないようですが、残念ながら LocalStrategy に関数 (ユーザー名、パスワード、完了) を入力していません。
したがって、私の問題は、oo javascript スタイルでミドルウェアを使用しようとする方法にあると思います。開始に使用したテンプレートは RedHat OpenShift Cloud からのもので、 https ://github.com/openshift/nodejs-custom-version-openshift/blob/master/server.js で確認できます。
パスポートミドルウェアを使用しようとしたサーバーの初期化方法は次のとおりです。
self.initializeServer = function() {
self.createGetRoutes();
self.createPostRoutes();
self.app = express.create();
passport.use(new LocalStrategy(
function(username, password, done) {
console.log("TEST");
process.nextTick(function () {
console.log('Here I am!');
return done(null, user);
});
}
));
self.app.configure(function() {
self.app.use(passport.initialize());
});
// Paths without authentication
self.app.get('/holy', function(req, res) {res.send('SHIT! \n')});
// Add GET handlers for the app with authentication (from the getRoutes).
for (var g in self.getRoutes) {
self.app.get(g, passport.authenticate('local', { session: false }), self.getRoutes[g]);
}
// Add POST handlers for the app with authentication (from the postRoutes).
for (var p in self.postRoutes) {
self.app.post(p, passport.authenticate('local', { session: false }), self.postRoutes[p]);
}
};