如何在Keystone.js项目中使用Express.js中间件?

时间:2015-02-23 22:50:16

标签: javascript node.js express keystonejs

我目前正在构建一个Keystone.js项目,并且需要使用一些Express.js中间件库。由于Keystone.js是建立在Express.js之上的,这看起来很容易,但是到目前为止我遇到了很多问题。

这是我目前正在尝试的内容(这是我最好的猜测正确的方法):

在我的keystone.js文件(应用程序的主要入口点)中,我在keystone.start()之前直接插入以下代码:

keystone.app.use(stormpath.init(keystone.app, {
  ...
}));

这里的重要位是keystone.app.use(...);位 - 我看了一下Keystone.js源代码,看来底层的Express.js应用程序对象公开为keystone.app,这是为什么我试图以这种方式使用它。

不幸的是,当我的Keystone Web服务器在运行$ node keystone.js时启动,尝试加载我网站上的任何页面会导致以下异常:

$ node keystone.js

------------------------------------------------
KeystoneJS Started:
keystone is ready on port 3000
------------------------------------------------

TypeError: Object #<Object> has no method 'regenerate'
    at doSignin (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/lib/session.js:38:15)
    at Promise.<anonymous> (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/lib/session.js:72:5)
    at Promise.<anonymous> (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
    at Promise.EventEmitter.emit (events.js:95:17)
    at Promise.emit (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
    at Promise.fulfill (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
    at Promise.resolve (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/lib/promise.js:114:23)
    at Promise.<anonymous> (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
    at Promise.EventEmitter.emit (events.js:95:17)
    at Promise.emit (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
    at Promise.fulfill (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
    at /Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/lib/query.js:1400:13
    at model.Document.init (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/lib/document.js:250:11)
    at completeOne (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/lib/query.js:1398:10)
    at Object.cb (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/lib/query.js:1155:11)
    at Object._onImmediate (/Users/rdegges/Dropbox/Code/testing/keystone/node_modules/keystone/node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16)

我已经让MongoDB在本地运行得很好,如果我在上面注释掉我的代码,事情按预期工作,所以我知道这是根本原因。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

在使用&#39; app&#39;初始化keystone之前,您可以将已经存在的快速实例传递给keystone。基石上的物体。您可以在这里阅读:

http://keystonejs.com/docs/configuration/#options-concepts

来自更新文档评论的EDIT *:

https://keystonejs.com/documentation/configuration/project-options

Theres甚至还有一些额外的文字描述了如何传递你自己的快递实例以及它如何随着更新版本的快递而改变。

var express = require('express'),
app = express(),
keystone = require('keystone'),
serve = require('serve-static'),
favicon = require('serve-favicon'),
body = require('body-parser'),
cookieParser = require('cookie-parser'),
multer = require('multer');

var cookieSecret = 'secretCookie'

//Add your middleware
app.use(cookieParser(cookieSecret));
app.use(body.urlencoded({ extended: true }));
app.use(body.json());
app.use(multer());
keystone.init({
  'name': 'Website Name',
  'brand': 'Website Brand',
  'session': false,
  'updates': 'updates',
  'auth': true,
  'user model': 'User',
  'auto update': true,
  'cookie secret': cookieSecret
});

// Let keystone know where your models are defined. Here we have it at the `/models`
keystone.import('models');

// Serve your static assets
app.use(serve('./public'));

// This is where your normal routes and files are handled
app.get('/', function(req, res, next) {
  res.send('hello world');
});
keystone.app = app;
keystone.start();

然后keystone将使用您的express实例,这将允许您在初始化keystone之前添加所有中间件,就像它自己表达一样。