Skip to content

Commit 50815b7

Browse files
committed
SSL connection
1 parent 0ae801b commit 50815b7

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ SESSION_SECRET=
33

44
# URL for your local mongodb installation. The below value is likely correct unless you setup your MongoDB configuration differently.
55
LOCAL_MONGODB_URL=mongodb://localhost:27017
6+
# Certificate - needed for connecting to IBM Cloud Databases for MongoDB
7+
CERTIFICATE_BASE64=
8+
PORT=6001
9+
BIND=0.0.0.0

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ This application uses the [IBM Cloud Databases for MongoDB service](https://clou
3131
6. Run `node server.js` to start your app
3232
7. Open a browser to the link provided in the terminal prompt to view your app
3333

34+
Note that the code assumes a secured connection to MongoDB using SSL and a certificate. This way you can run the app locally but connect to IBM Cloud Databases for MongoDB.
35+
3436
##### Deploy to IBM Cloud
3537

3638
Option 1 (launch this app directly from this repo):

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.1.0",
44
"description": "MEAN (Mongo, Express, Angular, Node.js) Boilerplate for IBM Cloud",
55
"main": "server.js",
6-
"engines" : { "node" : "6.*" },
6+
"engines" : { "node" : "8.*" },
77
"scripts": {
88
"start": "node server.js",
99
"dev": "nodemon server.js"

server.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ if(appEnv.isLocal){
3636

3737
//Detects environment and connects to appropriate DB
3838
if(appEnv.isLocal){
39-
mongoose.connect(process.env.LOCAL_MONGODB_URL);
39+
var ca = Buffer.from(process.env.CERTIFICATE_BASE64, 'base64');
40+
mongoDbOptions = {
41+
useNewUrlParser: true,
42+
ssl: true,
43+
sslValidate: true,
44+
sslCA: ca
45+
};
46+
mongoose.connect(process.env.LOCAL_MONGODB_URL, mongoDbOptions)
47+
.then(res => console.log(res))
48+
.catch(function (reason) {
49+
console.log('Unable to connect to the mongodb instance. Error: ', reason);
50+
});
4051
sessionDB = process.env.LOCAL_MONGODB_URL;
4152
console.log('Your MongoDB is running at ' + process.env.LOCAL_MONGODB_URL);
4253
}
@@ -48,12 +59,22 @@ else if(!appEnv.isLocal) {
4859
mongoDbUrl = mongoDbCredentials.composed[0];
4960
mongoDbOptions = {
5061
useNewUrlParser: true,
62+
ssl: true,
63+
sslValidate: true,
64+
sslCA: ca,
5165
poolSize: 1,
5266
reconnectTries: 1
5367
};
5468

5569
console.log("Your MongoDB is running at ", mongoDbUrl);
56-
mongoose.connect(mongoDbUrl, mongoDbOptions); // connect to our database
70+
// connect to our database
71+
mongoose.Promise = global.Promise;
72+
mongoose.connect(mongoDbUrl, mongoDbOptions)
73+
.then(res => console.log(res))
74+
.catch(function (reason) {
75+
console.log('Unable to connect to the mongodb instance. Error: ', reason);
76+
});
77+
//mongoose.connect(mongoDbUrl, mongoDbOptions); // connect to our database
5778
sessionDB = mongoDbUrl;
5879
}
5980
else{
@@ -305,6 +326,6 @@ app.get('/protected', authorizeRequest, function(req, res){
305326
/********************************
306327
Ports
307328
********************************/
308-
app.listen(appEnv.port, appEnv.bind, function() {
329+
app.listen(process.env.PORT || appEnv.port, process.env.BIND || appEnv.bind, function() {
309330
console.log("Node server running on " + appEnv.url);
310331
});

0 commit comments

Comments
 (0)