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

标签 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。

感谢您的帮助

最佳答案

该 github 指南(或其为旧版本的 keystone 编写的指南)中似乎存在一些错误。也就是说,下面两行会引起麻烦:

keystone.static(app);

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

我将它们注释掉,并将数据库指向我的本地主机 mongoDB。我还从 yo 生成的 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'));

关于node.js - 如何将 Keystone.JS 添加到现有 Express.js 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30065764/

相关文章:

node.js - 如何访问 promise 中的值(value)?

node.js - NodeJS + CouchDB : Migrate from HTTP to HTTPS

javascript - Node 101 : app. 使用 ReferenceError [route] 未定义

reactjs - API 未返回正确的 JSON (react/express/axios)

node.js - express-rate-limit 阻止所有用户的请求

node.js - 遍历jade/pugjs 中的对象数组

node.js - 启动时在 Docker 容器中创建环境变量

javascript - promise 回调返回 promise

javascript - 显示 package.json 中的依赖项

node.js - 如何在 Keystone.js 数据库 ODM 中创建字符串数组?