简体   繁体   English

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

[英]How Can I Use Express.js Middleware in a Keystone.js Project?

I'm currently building a Keystone.js project, and need to use some Express.js middleware libraries with it. 我目前正在构建一个Keystone.js项目,需要使用一些Express.js中间件库。 Since Keystone.js is built on top of Express.js, this seemed like it would be fairly easy, but I'm running into lots of issues getting things working so far. 由于Keystone.js是建立在Express.js之上的,这看起来很容易,但是到目前为止我遇到了很多问题。

Here's what I'm currently trying (this is my best guess as to the correct way to do this): 这是我目前正在尝试的(这是我最好的猜测正确的方法):

In my keystone.js file (the app's main entrypoint), I'm inserting the following code directly before keystone.start() : 在我的keystone.js文件(应用程序的主入口点)中,我在keystone.start()之前直接插入以下代码:

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

The important bit here is the keystone.app.use(...); 这里重要的一点是keystone.app.use(...); bit -- I took a look at the Keystone.js source, and it appears that the underlying Express.js application object is exposed as keystone.app , which is why I'm attempting to use it this way. 我看了一下Keystone.js源代码,看来底层的Express.js应用程序对象是作为keystone.app公开的,这就是我试图以这种方式使用它的原因。

Unfortunately, while my Keystone web server starts when running $ node keystone.js , trying to load any page on my site results in the following exception: 不幸的是,当我的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)

I've got MongoDB running locally just fine, and if I comment out my code above things work as expected, so I know this is the root cause. 我已经在本地运行了MongoDB,如果我在上面注释掉我的代码,事情按预期工作,所以我知道这是根本原因。

Any help would be appreciated. 任何帮助,将不胜感激。

You can pass an already existing express instance into keystone before you initialise keystone using the 'app' object on keystone. 在使用keystone上的“app”对象初始化keystone之前,可以将已存在的express实例传递给keystone。 Which you can read about here: 您可以在这里阅读:

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

EDIT* from comments for updated docs: 编辑*来自更新文档的评论:

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

Theres even a little extra bit of text describing how to pass in your own express instance and how it changed with newer versions of express. 甚至还有一点点文字描述了如何传递你自己的快递实例以及它如何随着更新版本的快递而改变。

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();

Then keystone will use your instance of express which will allow you to add all the middleware as if it is express on its own before you initialise keystone. 然后keystone将使用您的express实例,这将允许您在初始化keystone之前添加所有中间件,就像它自己表达一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM