无法在 Openshift 上 运行 keystone node.js 应用程序
Unable to run keystone node.js application on Openshift
编辑 2 - 最后的笔记
下面的解决方案对我有用,但是您仍然需要手动指定上面显示的 mongo 连接凭据,以便应用程序正确 运行,否则您将得到 mongo授权错误。
编辑 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,但惨遭失败。
我已经 mongo 通过将此添加到 keystone.js 进行连接:
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 实例上的 env,它们似乎是正确的变量,我正在获取端口 8080 并且似乎是正确的 IP 地址。
我也尝试过对端口和地址部分进行硬编码,但它似乎没有什么区别,而且对于本地测试来说也不太可行。
我显然遗漏了一些简单的东西,非常感谢您的帮助!
谢谢
编辑 1 - 根据@GarethJeanne 的评论忽略 server.js 代码
如果您的切入点是 keystone.js
并且您根本没有使用 server.js
(这使我之前的回答变得无声),那么您需要确保您使用的是 Keystone 0.3.3
或更高版本。
此问题已在 2 月 28 日合并的 Pull Request 1127 中得到解决,并首次包含在 3 月 8 日发布的 0.3.3
版本中。
我认为这不是 Keystone 问题。您显然没有使用 Keystone 开箱即用的初始化和启动,因此 Keystone 正在处理您的 Express 应用程序的创建或初始化。
由于您没有共享所有代码(例如 self.initializeServer()
),我假设您正确设置了 Express 并将其连接到您的 Keystone 应用程序实例。
我同样假设 self.app
是 Express 的一个实例。
从您提交的代码中,我看到的唯一问题是您将 'self.port'
和 'self.ipaddress'
传递给 self.app.listen()
,每个参数都用单引号引起来.
这肯定会导致 listen EACCES
错误。
尝试删除 self.app.listen()
调用中 'self.port'
和 'self.ipaddress'
周围的单引号。
self.start = 函数() {
// 在特定接口(和端口)上启动应用程序。
self.app.listen(self.port, self.ipaddress, 函数() {
console.log('%s: 节点服务器启动于 %s:%d ...',
日期(Date.now()), self.ipaddress, self.port);
});
};
我不能保证你的应用程序的其余部分会正常工作,但至少应该消除 listen EACCES
错误。
编辑 2 - 最后的笔记
下面的解决方案对我有用,但是您仍然需要手动指定上面显示的 mongo 连接凭据,以便应用程序正确 运行,否则您将得到 mongo授权错误。
编辑 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,但惨遭失败。
我已经 mongo 通过将此添加到 keystone.js 进行连接:
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 实例上的 env,它们似乎是正确的变量,我正在获取端口 8080 并且似乎是正确的 IP 地址。
我也尝试过对端口和地址部分进行硬编码,但它似乎没有什么区别,而且对于本地测试来说也不太可行。
我显然遗漏了一些简单的东西,非常感谢您的帮助!
谢谢
编辑 1 - 根据@GarethJeanne 的评论忽略 server.js 代码
如果您的切入点是 keystone.js
并且您根本没有使用 server.js
(这使我之前的回答变得无声),那么您需要确保您使用的是 Keystone 0.3.3
或更高版本。
此问题已在 2 月 28 日合并的 Pull Request 1127 中得到解决,并首次包含在 3 月 8 日发布的 0.3.3
版本中。
我认为这不是 Keystone 问题。您显然没有使用 Keystone 开箱即用的初始化和启动,因此 Keystone 正在处理您的 Express 应用程序的创建或初始化。
由于您没有共享所有代码(例如 self.initializeServer()
),我假设您正确设置了 Express 并将其连接到您的 Keystone 应用程序实例。
我同样假设 self.app
是 Express 的一个实例。
从您提交的代码中,我看到的唯一问题是您将 'self.port'
和 'self.ipaddress'
传递给 self.app.listen()
,每个参数都用单引号引起来.
这肯定会导致 listen EACCES
错误。
尝试删除 self.app.listen()
调用中 'self.port'
和 'self.ipaddress'
周围的单引号。
我不能保证你的应用程序的其余部分会正常工作,但至少应该消除 listen EACCES
错误。