-1

编辑 2 - 最后的笔记

下面的解决方案对我有用,但是您仍然需要手动指定如上所示的 mongo 连接凭据才能使应用程序正确运行,否则您将收到 mongo auth 错误。

编辑 1 - 添加了完整的 keystone.js

keystone.js

// Simulate config options from your production environment by
// customising the .env file in your project's root folder.
require('dotenv').load();

// Require keystone
var keystone = require('keystone');

var mongoDbConnectionString =  process.env.OPENSHIFT_MONGODB_DB_URL || 'mongodb://localhost/********';

var host = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";

var port = process.env.OPENSHIFT_NODEJS_PORT  || process.env.OPENSHIFT_INTERNAL_PORT || 3000;

// Initialise Keystone with your project's configuration.
// See http://keystonejs.com/guide/config for available options
// and documentation.

keystone.init({

    'name': '********',
    'brand': '********',

    'less': 'public',
    'static': 'public',
    'favicon': 'public/favicon.ico',
    'views': 'templates/views',
    'view engine': 'jade',

    'emails': 'templates/emails',
    'mongo': mongoDbConnectionString,
    'host': host,
    'port':  port,
    'auto update': true,
    'session': true,
    'auth': true,
    'user model': 'User',
    'cookie secret': '********'


});

// Load your project's Models

keystone.import('models');

// Setup common locals for your templates. The following are required for the
// bundled templates and layouts. Any runtime locals (that should be set uniquely
// for each request) should be added to ./routes/middleware.js

keystone.set('locals', {
    _: require('underscore'),
    env: keystone.get('env'),
    utils: keystone.utils,
    editable: keystone.content.editable
});

// Load your project's Routes

keystone.set('routes', require('./routes'));

// Setup common locals for your emails. The following are required by Keystone's
// default email templates, you may remove them if you're using your own.

keystone.set('email locals', {
    logo_src: '/images/logo-email.gif',
    logo_width: 194,
    logo_height: 76,
    theme: {
        email_bg: '#f9f9f9',
        link_color: '#2697de',
        buttons: {
            color: '#fff',
            background_color: '#2697de',
            border_color: '#1a7cb7'
        }
    }
});

// Setup replacement rules for emails, to automate the handling of differences
// between development a production.

// Be sure to update this rule to include your site's actual domain, and add
// other rules your email templates require.

keystone.set('email rules', [{
    find: '/images/',
    replace: (keystone.get('env') == 'production') ? 'http://www.your-server.com/images/' : 'http://localhost:3000/images/'
}, {
    find: '/keystone/',
    replace: (keystone.get('env') == 'production') ? 'http://www.your-server.com/keystone/' : 'http://localhost:3000/keystone/'
}]);

// Load your project's email test routes

keystone.set('email tests', require('./routes/emails'));

// Configure the navigation bar in Keystone's Admin UI

keystone.set('nav', {
    'posts': ['posts', 'post-categories'],
    'creations': 'galleries',
    'contact us': 'enquiries',
    'users': 'users'
});

// Start Keystone to connect to your database and initialise the web server

    console.log('%s: IP Set.', host);
    console.log('%s: Port Set.', port);

keystone.start();

我正在尝试构建我的第一个 Keystone.js,让它在我的机器上本地运行良好。

现在,我正试图将我的网站推送到 Openshift 并且惨遭失败。

我已经通过将它添加到 keystone.js 来连接 mongo:

var mongoDbConnectionString =  process.env.OPENSHIFT_MONGODB_DB_URL || 'mongodb://localhost/*********';

但是我无法让事情正常运行,因为它似乎在绑定到我在 Openshift 上提供的 ip 和端口时遇到问题,我正在使用以下代码:

var host = process.env.OPENSHIFT_NODEJS_IP || process.env.OPENSHIFT_INTERNAL_IP || "127.0.0.1";

var port = process.env.OPENSHIFT_NODEJS_PORT  || process.env.OPENSHIFT_INTERNAL_PORT || 3000;

结合:

keystone.init({

'name': '*********',
'brand': '*********',

'less': 'public',
'static': 'public',
'favicon': 'public/favicon.ico',
'views': 'templates/views',
'view engine': 'jade',

'emails': 'templates/emails',
'mongo': mongoDbConnectionString,
'host': host,
'port':  port,
'auto update': true,
'session': true,
'auth': true,
'user model': 'User',
'cookie secret': '*********'


});

但我不断得到:

==> app-root/logs/nodejs.log <==
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
....

with 'node keystone.js'
DEBUG: Sending SIGTERM to child...

现在我检查了 Openshift 实例上的环境,它们似乎是正确的变量,我正在获取端口 8080,并且似乎是正确的 IP 地址。

我也尝试过对端口和地址部分进行硬编码,但似乎没有什么区别,而且对于本地测试也不是真正可行的。

我显然在这里遗漏了一些简单的东西,非常感谢您的帮助!

谢谢

4

1 回答 1

0

编辑 1 - 根据@GarethJeanne 的评论忽略 server.js 代码

如果您的入口点是keystone.js并且您根本没有使用server.js(这使我之前的答案静音),那么您需要确保您使用的是 Keystone0.3.3或更新版本。

此问题已在 2 月 28 日合并的Pull Request 11270.3.3中得到解决,并首次包含在3 月 8 日发布的 version 中。

我不认为这是 Keystone 问题。您显然没有使用 Keystone 的开箱即用初始化和启动,因此 Keystone 正在处理您的 Express 应用程序的创建或初始化。

由于您没有共享所有代码(例如self.initializeServer()),我假设您正确设置 Express 并将其连接到您的 Keystone 应用程序实例。

我同样会假设self.app是 Express 的一个实例。

从您提交的代码中,我看到的唯一问题是您正在传递'self.port''self.ipaddress'self.app.listen()每个参数周围加上单引号。

这肯定会导致listen EACCES错误。

尝试删除通话中'self.port'和周围的单引号'self.ipaddress'self.app.listen()

self.start = function() { // 在特定接口(和端口)上启动应用程序。self.app.listen(self.port, self.ipaddress, function() { console.log('%s: Node server started on %s:%d ...', Date(Date.now() ), self .ipaddress, self.port); }); };

我不能保证您的应用程序的其余部分都能正常工作,但它至少应该消除listen EACCES错误。

于 2015-03-07T23:11:49.607 回答