如何将Keystone.JS添加到现有的Express.js应用程序?

时间:2015-05-06 01:03:58

标签: node.js express keystonejs

我在入门指南中看到了this on githubthis section。但我无法让管理员显示(localhost:3000/keystone返回404)

我只是希望能够访问管理员并编辑数据。所以我在app.js中添加了以下内容

var keystone = require('keystone');
keystone.set('app', app);
keystone.set('mongoose', mongoose);
keystone.init({
    'name': 'project',
    'auth': true,
    'user model': 'user',
    'mongo': process.env.MONGOLAB_URI || dbConfig.url,
    'session': true,
    'cookie secret': 'loUGL*gbp98bPIUBI*UY'
});
keystone.import('models');
keystone.routes(app);

我正在使用Node 0.12+和Express 4。

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

github指南中似乎有一些错误(或者是为旧版本的keystone编写的)。也就是说,以下两行会造成麻烦:

keystone.static(app);

keystone.mongoose.connect.on('error', handleDbErrorsFunc);

我评论了这些,并将数据库指向我的localhost mongoDB。我还从一个你生成的keystone项目中处理了models/User.js。那个,我有管理控件工作(虽然没有任何CSS)

如果您想比较笔记,这是我编辑的指南版本:

var express = require('express'),
    app = express(),
    keystone = require('keystone'),
    session = require('express-session'),
    flash = require('connect-flash'),
    serve = require('serve-static'),
    favicon = require('serve-favicon'),
    body = require('body-parser'),
    mongoose = require('mongoose'),
    cookieParse = require('cookie-parser'),
    multer = require('multer');

app.set('port', process.env.PORT || 9000);
app.set('view engine', 'jade');
// app.use(favicon(__dirname + '/public/images/favicon.ico'));
app.use(cookieParse());
app.use(body.urlencoded({
    extended: true
}));
app.use(body.json());
app.use(multer());

//Session and flash are required by keystone
app.use(flash());
app.use(session({
    secret: 'Keystone is the best!',
    resave: false,
    saveUninitialized: true
}));

keystone.app = app;
keystone.mongoose = mongoose;
keystone.init({
    'user model': 'User',
    'mongo': 'mongodb://localhost/keystone',
    'session': true,
    'static': 'public'
});

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

// Set keystone's to serve it's own public files. for instance, its logo's and stylesheets
// keystone.static(app);

// Set keystone routes for the admin panel, located at '/keystone'
keystone.routes(app);

// Initialize keystone's connection the database
keystone.mongoose.connect(keystone.get('mongo'));

// Create a handler for DB connection errors
// keystone.mongoose.connect.on('error', handleDbErrorsFunc);

// 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');
});

// Start your express server
app.listen(app.get('port'));

答案 1 :(得分:1)

在关注this guide时我也遇到了这个问题。我发现通过删除行keystone.app = app;,管理面板可以正常工作。那么最终对我有用的是取代keystone.app = app;keystone.set('routes', app);

相关问题