Skip to content

Commit d38f492

Browse files
authored
Merge pull request #26 from IBM-Cloud/icd-mongoDB
More changes to the app
2 parents 1850086 + 7824234 commit d38f492

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

.env.example

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# This value is the salt used to encrypt sessions. Makeup a long string of text for this value. This is a MANDATORY value for your application.
22
SESSION_SECRET=
33

4-
# URL for your local mongodb installation. The below value is likely correct unless you setup your MongoDB configuration differently.
5-
LOCAL_MONGODB_URL=mongodb://localhost:27017
4+
# URL for your mongodb instance on IBM Cloud.
5+
MONGODB_URL=
6+
67
# Certificate - needed for connecting to IBM Cloud Databases for MongoDB
78
CERTIFICATE_BASE64=
8-
PORT=6001
9+
PORT=6020
910
BIND=0.0.0.0

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ COPY ./server.js /src/server.js
2020
COPY ./.env /src/.env
2121

2222

23-
EXPOSE 6001
23+
EXPOSE 6020
2424
CMD [ "npm", "start" ]

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,29 @@ This application uses the [IBM Cloud Databases for MongoDB service](https://clou
2222
- [Cloud Foundry Command Line Tool](https://docs.cloudfoundry.org/devguide/installcf/)
2323

2424
## Getting Started
25-
##### Local Application Development
25+
##### Run the application locally
2626
1. Clone or download this repo onto your machine.
27-
2. Install [application requirements](#application-requirements) if not done so already.
28-
3. Open application directory in your terminal and run `npm install`
29-
4. Rename `.env.example` file to `.env`. Edit the contents as needed, at a minimum adding your own SESSION_SECRET.
30-
5. Start up your local MongoDB server (typically just `mongod`, see docs [here](https://docs.mongodb.org/getting-started/shell/installation/))
31-
6. Run `node server.js` to start your app
32-
7. Open a browser to the link provided in the terminal prompt to view your app
33-
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-
36-
An alternative way of local development is using the provided Dockerfile.
27+
1. Install [application requirements](#application-requirements) if not done so already.
28+
1. Open application directory in your terminal and run `npm install`
29+
1. If you don't have an account, [create a free one here](https://cloud.ibm.com).
30+
1. Login to your account via the command line: `ibmcloud login`
31+
1. Target your account ORG and SPACE `ibmcloud target --cf`
32+
1. Create the instance of Compose for MongoDB on IBM Cloud: `ibmcloud cf create-service databases-for-mongodb standard mean-starter-mongodb`
33+
1. Rename `.env.example` file to `.env` and run `ibmcloud cf service-key mean-starter-mongodb "Service credentials-1"` for MONGODB_URL and CERTIFICATE_BASE64. Choose your own SESSION_SECRET.
34+
1. Run `node server.js` to start your app
35+
1. Open a browser to the link provided in the terminal prompt to view your app
36+
37+
> 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.
38+
39+
An alternative way of running locally is using the provided `Dockerfile`.
40+
- Install docker on your machine and build the docker image
41+
```
42+
docker build . -t mean-stack:v1.0.0
43+
```
44+
- Run the app locally
45+
```
46+
docker run -p 6020:6020 -ti mean-stack:v1.0.0
47+
```
3748

3849
##### Deploy to IBM Cloud
3950

@@ -52,11 +63,9 @@ Option 2 (deploy from your local machine):
5263
7. Push your app to IBM Cloud with `ibmcloud cf push`
5364
8. Done, the app should be looking like:<img src="ReadME-Images/live-app.png">
5465

55-
56-
5766
#### Problems or Questions?
5867

59-
Create a [GitHub issue](https://github.com/IBM-Cloud/nodejs-MEAN-stack/issues/new) for questions or problems occurs using this demo.
68+
Create a [GitHub issue](https://github.com/IBM-Cloud/nodejs-MEAN-stack/issues/new) for questions or problems occurs using this demo.
6069

6170
## Critical Files & Folders
6271

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var express = require('express'),// server middleware
2121
appEnv = cfenv.getAppEnv(),// Grab environment variables
2222

2323
User = require('./server/models/user.model');
24-
24+
2525

2626
/********************************
2727
Local Environment Variables
@@ -30,26 +30,26 @@ if(appEnv.isLocal){
3030
require('dotenv').load();// Loads .env file into environment
3131
}
3232

33-
/********************************
33+
/********************************
3434
MongoDB Connection
3535
********************************/
3636

3737
//Detects environment and connects to appropriate DB
3838
if(appEnv.isLocal){
3939
var ca = Buffer.from(process.env.CERTIFICATE_BASE64, 'base64');
4040
mongoDbOptions = {
41-
useNewUrlParser: true,
41+
useNewUrlParser: true,
4242
ssl: true,
4343
sslValidate: true,
4444
sslCA: ca
4545
};
46-
mongoose.connect(process.env.LOCAL_MONGODB_URL, mongoDbOptions)
46+
mongoose.connect(process.env.MONGODB_URL, mongoDbOptions)
4747
.then(res => console.log(res))
4848
.catch(function (reason) {
4949
console.log('Unable to connect to the mongodb instance. Error: ', reason);
5050
});
51-
sessionDB = process.env.LOCAL_MONGODB_URL;
52-
console.log('Your MongoDB is running at ' + process.env.LOCAL_MONGODB_URL);
51+
sessionDB = process.env.MONGODB_URL;
52+
console.log('Your MongoDB is running at ' + process.env.MONGODB_URL);
5353
}
5454
// Connect to MongoDB Service on IBM Cloud
5555
else if(!appEnv.isLocal) {

0 commit comments

Comments
 (0)