VMWare CloudFoundry practice (2): successful connection with MongoDB

Source: Internet
Author: User
Tags install mongodb mongodb support
To put it simply, this code defines the mongo object at the beginning. This if-else is used to make the program available both on the cloud and locally. if it is on the cloud, the if branch line is used.

To put it simply, this code defines the mongo object at the beginning. This if-else is used to make the program available both on the cloud and locally. if it is on the cloud, the if branch line is used.

In the evening, I continued to follow the tutorial and completed the connection to MongoDB. There were some slight bumps in the middle.

1. Follow this tutorial:

2. Download and install MongoDB:

3. Create a folder named mongo, create the main. js page, and write the following code:

To put it simply, this code defines the mongo object at the beginning. This if-else is designed to make this application available both on the cloud and locally. if it is on the cloud, it will go through the if branch line, mongo obtains the configuration from VCAP_SERVICES. If it is local, take the else branch. The mongodb address is localhost. Then, concatenate the mongoDB URL using the generate_mongo_url () function. After obtaining the user's remote address, add the user to the database and display it in the browser.

// Mongodb-65b2c
If (process. env. VCAP_SERVICES ){
Var env = JSON. parse (process. env. VCAP_SERVICES );
Var mongo = env ['mongodb-2.0 '] [0] ['credentials'];
}
Else {
Var mongo = {
"Hostname": "localhost ",
"Port": 27017,
"Username ":"",
"Password ":"",
"Name ":"",
"Db": "db"
}
}

Var generate_cmd_url = function (obj ){
Obj. hostname = (obj. hostname | 'localhost ');
Obj. port = (obj. port | 27017 );
Obj. db = (obj. db | 'test ');

If (obj. username & obj. password ){
Return "mongodb: //" + obj. username + ":" + obj. password + "@" + obj. hostname + ":" + obj. port + "/" + obj. db;
}
Else {
Return "mongodb: //" + obj. hostname + ":" + obj. port + "/" + obj. db;
}
}

Var response url = generate_response _url (mongo );

Var record_visit = function (req, res ){
/* Connect to the DB and auth */
Require ('mongodb '). connect (Response URL, function (err, conn ){
Conn. collection ('ips ', function (err, coll ){
/* Simple object to insert: ip address and date */
Object_to_insert = {'IP': req. connection. remoteAddress, 'ts': new Date ()};

/* Insert the object then print in response */
/* Note the _ id has been created */
Coll. insert (object_to_insert, {safe: true}, function (err ){
Res. writeHead (200, {'content-type': 'text/plain '});
Res. write (JSON. stringify (object_to_insert ));
Res. end ('\ n ');
});
});
});
}

Var http = require ('http ');

Http. createServer (function (req, res ){
Record_visit (req, res );
}). Listen (3000 );

4. Another important step is to execute npm install mongodb in the project directory. This will download some files in the project folder, that is, the mongodb support library.

At first, I executed npm install mongodb on another console, and thought it was effective for the whole world. Later, when I deployed it, I was unable to pass the test, and failed to Checking MongoDB!

5. Now you can deploy the application. Use vmc update to publish the application. Note: When asked whether additional services are required, You must select yes. Then, CF cloud will help you add MongoDB support.

6. test:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.