How can I use Express.js middleware in a Keystone.js project?

I am currently creating a Keystone.js project and you need to use Express.js with it. Since Keystone.js is built on top of Express.js, it seemed like it would be pretty easy, but I have a lot of problems when everything works.

Here is what I'm trying now (this is my best guess as to the correct way to do this):

In my file keystone.js(the main entry point of the application) I insert the following code immediately before keystone.start():

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

The bit keystone.app.use(...);is important here - I took a look at the source of Keystone.js and it seems that the main object of the Express.js application is displayed as keystone.app, so I'm trying to use it that way.

Unfortunately, while my Keystone web server starts at startup $ node keystone.js, an attempt to load any page on my site results in the following exception:

$ 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 have MongoDB working locally just fine, and if I comment on my code above everything works as expected, so I know this is the main reason.

Any help would be appreciated.

+4
source share
2 answers

You can transfer an existing express instance to the key path before initiating a keystone search using the app object on the key background. What you can read here:

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

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

keystone express, , , .

+3

?

npm install -g
-5

All Articles