stormpath error 如果不指定 \'requestAuthenticator\' 字段,y

stormpath error If you do not specify a \'requestAuthenticator\' field, y

我在尝试构建 express-stormpath 应用程序时遇到上述错误。我将列出适用的代码:

  Error: If you do not specify a 'requestAuthenticator' field, you must specify an ApiKey.
         at Object.getAuthenticator (d:\dev\git-repos\bps\VolumeGridDataEntry\node_modules\express-    stormpath\node_modules\stormpath\lib\authc\index.js:24:11)
         at new RequestExecutor (d:\dev\git-repos\bps\VolumeGridDataEntry\node_modules\express-stormpath\node_modules\stormpath\lib\ds\RequestExecutor.js:37:37)
          at new DataStore (d:\dev\git-repos\bps\VolumeGridDataEntry\node_modules\express-stormpath\node_modules\stormpath\lib\ds\DataStore.js:46:52)
          at new Client (d:\dev\git-repos\bps\VolumeGridDataEntry\node_modules\express-stormpath\node_modules\stormpath\lib\Client.js:8:21)
         at d:\dev\git-repos\bps\VolumeGridDataEntry\node_modules\express-stormpath\lib\stormpath.js:60:36
         at d:\dev\git-repos\bps\VolumeGridDataEntry\node_modules\express-stormpath\node_modules\stormpath\lib\authc\ApiKeyLoader.js:14:14
         at d:\dev\git-repos\bps\VolumeGridDataEntry\node_modules\express-stormpath\node_modules\stormpath\node_modules\properties-parser\index.js:348:20
         at fs.js:208:20
         at Object.oncomplete (fs.js:108:15)

[更新]server.js

  var express = require('express'),
      session = require('express-session'),
      crypto = require('crypto'),
      formidable = require('formidable'),
      path = require('path'),
      favicon = require('serve-favicon'),
      logger = require('morgan'),
      cookieParser = require('cookie-parser'),
      bodyParser = require('body-parser'),
      index = require('./routes/index'),
      users = require('./routes/users'),
      stormpath = require('express-stormpath'),
      config = require('./config/credentials.js'),
      app = express();

  function hashPwd(pwd) {
    return crypto.createHash('sha256').update(pwd).digest('base64').toString();
  }

  // view engine setup
 app.set('views', path.join(__dirname, 'views'));
 app.set('view engine', 'ejs');


 app.use(stormpath.init(app, {
    apiKeyFile: config.stormpathapi.apiKeyFile,
    application: config.stormpathapi.application,
    secretKey: config.stormpathapi.secretKey,
    sessionDuration: 1000 * 60 * 30
 }));

 // domains for better error handling
 app.use(function(req, res, next){
    // create a domain for this request
    var domain = require('domain').create();
    // handle errors on this domain
    domain.on('error', function(err){
       console.error('DOMAIN ERROR CAUGHT\n', err.stack);
       try {
           // failsafe shutdown in 5 seconds
           setTimeout(function(){
              console.error('Failsafe shutdown.');
              process.exit(1);
            }, 5000);

           // stop taking new requests
           server.close();

           try {
               // attempt to use Express error route
               next(err);
            } catch(error){
              // if Express error route failed, try
              // plain Node response
              console.error('Express error mechanism failed.\n', error.stack);
              res.statusCode = 500;
              res.setHeader('content-type', 'text/plain');
              res.end('Server error.');
            }
        } catch(error){
           console.error('Unable to send 500 response.\n', error.stack);
        }
    });

     // add the request and response objects to the domain
     domain.add(req);
     domain.add(res);

     // execute the rest of the request chain in the domain
     domain.run(next);
   });

   // cross-site request forgery protection
   app.use(require('csurf')());

   app.use(function(req, res, next){
      res.locals._csrfToken = req.csrfToken();
      next();
    });

    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'client')));

    //name: cookie_name,
    //store: sessionStore, // connect-mongo session store
    app.use(session({
        secret: config.cookieSecret,
        resave: false,
        saveUninitialized: true
     }));

     app.use('/', index);
     app.use('/users', users);

    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
         var err = new Error('Not Found');
         err.status = 404;
        next(err);
    });

    // error handlers

    // development error handler
    // will print stacktrace
    if (app.get('env') === 'development') {
       app.use(function(err, req, res, next) {
       res.status(err.status || 500);
       res.render('error', {
           message: err.message,
           error: err
        });
     });
   }

  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
          message: err.message,
          error: {}
      });
   });


  module.exports = app;

路线index.js

  var express = require('express'),
      stormpath = require('express-stormpath'),
      router = express.Router();

  router.get('/', stormpath.groupsRequired(['dataentry']), function(req, res) {
     res.render('index', { title: 'Volume Grid Data Entry' });
  });

 module.exports = router;

最初路由签名是这样的,但那也不起作用。我将来需要能够在组上建立页面安全性......所以,我不确定我是否应该使用 loginRequired 或 groupsRequired 或两者:-/

  router.get('/', stormpath.loginRequired, function(req, res) {

谢谢!

该代码看起来正确——但是您的 app.use(require('./index')); 代码在哪里?在上面的 app.use(stormpath.init(...)) 内容之后这将是必要的 =)

注意:我是 express-stormpath 库的作者。

编辑:这是一个完整的例子:

var express = require('express'),
    stormpath = require('express-stormpath'),
    router = express.Router();

var app = express();

router.get('/', stormpath.groupsRequired(['dataentry']), function(req, res) {
 res.render('index', { title: 'Volume Grid Data Entry' });
});

app.use('/', router);

app.listen(3000);