【问题标题】:How to add Keystone.JS to an exisiting Express.js app?如何将 Keystone.JS 添加到现有的 Express.js 应用程序中?
【发布时间】:2015-07-15 22:36:44
【问题描述】:

我在入门指南中看到了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。

感谢您的帮助

【问题讨论】:

    标签: node.js express keystonejs


    【解决方案1】:

    该 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'));
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-09-06
        • 1970-01-01
        • 1970-01-01
        • 2016-09-25
        • 2013-12-08
        • 1970-01-01
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        相关资源
        最近更新 更多