Skip to content

Commit 8c247e6

Browse files
authored
Merge pull request #462 from ajeetraina/master
Added New Mongo and NodeJS based Project
2 parents 650b7fa + 30ec7da commit 8c247e6

File tree

1,859 files changed

+291757
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,859 files changed

+291757
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ We recommend you to visit [Docker Awesome Compose Repository](https://github.com
103103
| MongoDB|
104104
|:-------------|
105105
| [NGINX + Flask + MongoDB](https://github.com/docker/awesome-compose/tree/master/nginx-flask-mongo) |
106-
106+
| [NodeJS + MongoDB](https://github.com/collabnix/dockerlabs/tree/master/solution/node-mongo-docker) |
107107

108108
<br>
109109

solution/node-mongo-docker/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM node:12.18.1
2+
ENV NODE_ENV=production
3+
4+
WORKDIR /app
5+
6+
COPY ["package.json", "package-lock.json*", "./"]
7+
8+
RUN npm install --production
9+
10+
COPY . .
11+
12+
CMD [ "node", "server.js" ]

solution/node-mongo-docker/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Docker Samples
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

solution/node-mongo-docker/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Creating a REST API using Node.JS, MongoDB and Docker
2+
3+
4+
## Building from Scratch
5+
6+
### Create a directory
7+
8+
```
9+
mkdir node-mongo-docker
10+
```
11+
12+
### Initialize
13+
14+
```
15+
cd node-mongo-docker
16+
npm init -y
17+
```
18+
19+
```
20+
Wrote to /../../node-mongo-docker/package.json:
21+
22+
{
23+
"name": "mongo-docker",
24+
"version": "1.0.0",
25+
"description": "",
26+
"main": "index.js",
27+
"scripts": {
28+
"test": "echo \"Error: no test specified\" && exit 1"
29+
},
30+
"repository": {
31+
"type": "git",
32+
"url": "git+https://github.com/dockersamples/node-mongo-docker.git"
33+
},
34+
"keywords": [],
35+
"author": "",
36+
"license": "ISC",
37+
"bugs": {
38+
"url": "https://github.com/dockersamples/node-mongo-docker/issues"
39+
},
40+
"homepage": "https://github.com/dockersamples/node-mongo-docker#readme"
41+
}
42+
```
43+
44+
45+
46+
47+
48+
```
49+
npm install ronin-server ronin-mocks
50+
51+
added 117 packages, and audited 118 packages in 6s
52+
53+
7 packages are looking for funding
54+
run `npm fund` for details
55+
56+
found 0 vulnerabilities
57+
```
58+
59+
60+
## Creating server.js
61+
62+
Now, let’s add some code to handle our REST requests. We’ll use a mock server so we can focus on Dockerizing the application.
63+
64+
Open this working directory in your IDE and add the following code into the server.js file.
65+
66+
67+
```
68+
const ronin = require('ronin-server')
69+
const mocks = require('ronin-mocks')
70+
71+
const server = ronin.server()
72+
73+
server.use('/', mocks.server(server.Router(), false, true))
74+
server.start()
75+
```
76+
77+
78+
## Testing the application
79+
80+
81+
Let’s start our application and make sure it’s running properly. Open your terminal and navigate to your working directory you created.
82+
83+
```
84+
node server.js
85+
```
86+
87+
Let's open the new terminal and test it
88+
89+
```
90+
curl --request POST \
91+
--url http://localhost:8000/test \
92+
--header 'content-type: application/json' \
93+
--data '{"msg": "testing" }'
94+
```
95+
96+
## Results:
97+
98+
```
99+
{"code":"success","payload":[{"msg":"testing","id":"fdf077e5-9606-471d-a5ab-e6ec11e7f3e4","createDate":"2022-09-07T09:38:49.818Z"}]}%
100+
```
101+
102+
## Bring up the containers
103+
104+
105+
```
106+
docker compose up -d
107+
```
108+
109+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
services:
2+
notes:
3+
build:
4+
context: .
5+
ports:
6+
- 8000:8000
7+
- 9229:9229
8+
environment:
9+
- SERVER_PORT=8000
10+
- CONNECTIONSTRING=mongodb://mongo:27017/notes
11+
volumes:
12+
- ./:/app
13+
command: npm run debug
14+
15+
mongo:
16+
image: mongo:4.2.8
17+
ports:
18+
- 27017:27017
19+
volumes:
20+
- mongodb:/data/db
21+
- mongodb_config:/data/configdb
22+
volumes:
23+
mongodb:
24+
mongodb_config:

solution/node-mongo-docker/node_modules/.bin/mime

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solution/node-mongo-docker/node_modules/.bin/nodemon

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solution/node-mongo-docker/node_modules/.bin/nodetouch

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solution/node-mongo-docker/node_modules/.bin/nopt

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)