gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:32 24 4
gpt4 key购买 nike

我见过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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com