简体   繁体   中英

How to create connection to mongodb in openshift using node.js

How can connect to mongodb within openshift. using something like this. MongoDB 2.4 database added.
Please make note of these credentials:

Root User: admin Root Password: kc18cxkk7c1qs Database Name: top

Connection URL: mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/ using the node.js program below, could someone please tell me how to set this up. and is it also possible to make the code to connect to both localhost and online. my openshift account look like this it-carlow210.rhcloud.com

thanks in advance

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;

It is recommended to use environment variable for username and password. I do like this:

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;

And then make connection with your preferred module...

const db = monk(dbURL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM