如何使用node.js在openshift中创建与mongodb的连接

时间:2016-04-09 01:33:05

标签: node.js mongodb openshift

如何在openshift中连接到mongodb。用这样的东西。 添加了MongoDB 2.4数据库 请记下这些凭据:

Root用户:admin    Root密码:kc18cxkk7c1qs    数据库名称:顶部

连接网址:mongodb:// $ OPENSHIFT_MONGODB_DB_HOST:$ OPENSHIFT_MONGODB_DB_PORT / 使用下面的node.js程序,有人可以告诉我如何设置它。并且还可以使代码连接到localhost和online。 我的openshift帐户看起来像这样 it-carlow210.rhcloud.com

提前致谢

var config = {};

config.crawler = {};
config.db = {};
config.parser = {};
config.web = {};

// Excluded extensions for crawling
//config.crawler.excludedUrlPatterns = [
//      "\\.(pdf|js|css|zip|docx|jpg|png|gif|woff|xml|rss)$"
//      ];
// which extension allow to crawl
config.crawler.allowedUrlPatterns = [
        "/[^./]*$" // extension less
        ,"\\.(html|htm|aspx|php)$" // .html + .htm
        ];
// List of content types to process
config.crawler.contentTypes = ["text/html"];
// crawler interval
config.crawler.interval = 300;
// crawler maxConcurrency
config.crawler.maxConcurrency = 2;
config.crawler.timeout = 20 * 1000; // 20 seconds
config.crawler.maxResourceSize = 1024 * 1024 * 1; // 1mb
config.crawler.customHeaders = {}; // { "Authorization" : "secret" };
config.crawler.acceptCookies = false;


config.db.mongo = {};
config.db.mongo.ip = process.env.IP || "localhost";
config.db.mongo.url = "mongodb://" + config.db.mongo.ip + ":27017/topic-aggregator";


// html "jquery style" selector for the body content (es. "body", "article", "div#text")
//  can be override on each site
config.parser.defaultContentSelector = "body";

// use port 3000 for listening (replace port number)
config.web.port = process.env.PORT || process.env.WEB_PORT || 3000;
config.web.ip = process.env.IP;

config.web.logRequests = false;

module.exports = config;

1 个答案:

答案 0 :(得分:0)

建议使用环境变量作为用户名和密码。我喜欢这个:

const env = process.env;
const dbName = '/tadejp';
const dbURL = (env.OPENSHIFT_MONGODB_DB_HOST) ? (env.OPENSHIFT_MONGODB_DB_USERNAME + ':' +env.OPENSHIFT_MONGODB_DB_PASSWORD+ '@' + env.OPENSHIFT_MONGODB_DB_HOST +':'+env.OPENSHIFT_MONGODB_DB_PORT+dbName ) : 'localhost:27017'+dbName;

然后与您首选的模块建立连接......

const db = monk(dbURL);