Skip to content

Commit 2d8b9d2

Browse files
Jaime Salas ZancadaJaime Salas Zancada
Jaime Salas Zancada
authored and
Jaime Salas Zancada
committed
added docker support for node
1 parent 0d39ef1 commit 2d8b9d2

File tree

12 files changed

+46
-12
lines changed

12 files changed

+46
-12
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ typings/
6060
# Build folders parcel
6161
dist/
6262
.cache
63+
64+
.DS_store

11 Axios/03_callbacks/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 3 Using callbacks
44

5-
### 1. Currently our bookService is doing several things, it is calling the server for data and then hndling the response, and displaying data. Lets start to honor SRP, and start by passing callbacks that could be passed from outsite of service.
5+
### 1. Currently our bookService is doing several things, it is calling the server for data handling the response, and displaying data. Lets start to honor SRP, and start by passing callbacks that could be passed from outsite of our service to segregate responsabilities.
66

77
```diff bookAPI.js
88
import axios from 'axios';

11 Axios/05_all/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
```javascript
66
import axios from 'axios';
77

8-
const BASE_URL = 'http://localhost:8000';
8+
const baseUrl = 'http://localhost:8000';
99

1010
export const getAuthors = () => (
11-
axios.get(`${BASE_URL}/api/authors`)
11+
axios.get(`${baseUrl}/api/authors`)
1212
);
1313
```
1414

11 Axios/06_multiple_params/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 6 Get Multiple Params
22

3-
### 1. Now we are going to perform a get operation with multiple params. For that in mind lets add a new page `src/pages/booksearch.html`
3+
### 1. Now we are going to perform a get operation with multiple params. With that on mind lets add a new page `src/pages/booksearch.html`
44

55
```html
66
<!doctype html>
@@ -49,13 +49,13 @@
4949
```diff
5050
import axios from 'axios';
5151

52-
const BASE_URL = 'http://localhost:8000';
52+
const baseUrl = 'http://localhost:8000';
5353

54-
export const getBooks = () => axios.get(`${BASE_URL}/api/books/`);
54+
export const getBooks = () => axios.get(`${baseUrl}/api/books/`);
5555
+
56-
+export const getBooksByParams = (params) => axios.get(`${BASE_URL}/api/books/`, params);
56+
+export const getBooksByParams = (params) => axios.get(`${baseUrl}/api/books/`, params);
5757
+
58-
export const postBook = (book) => axios.post(`${BASE_URL}/api/books/`, book);
58+
export const postBook = (book) => axios.post(`${baseUrl}/api/books/`, book);
5959
```
6060

6161
### 4. Create a new file pages/booksearch/main.js. This file will handle the retrieving params for our new operation.

13 Fetch/00_createRequestFactory/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export const retrieveBook = (bookId) => {
141141
});
142142
}
143143
```
144-
* Notice that fetch is prepared to always a promise, even the success result.
144+
* Notice that fetch is prepared to always return a promise, even the success result.
145145

146146
### 5. Now let's define the src/js/main.js and check that is working on happy path
147147

docker-compose.yml

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
version: '3.7'
22
services:
3+
app:
4+
container_name: book-server
5+
environment:
6+
- ENVIRONMENT=mongodb:27017
7+
build: ./server-mongo
8+
ports:
9+
- "8000:8000"
10+
depends_on:
11+
- mongodb
312
mongodb:
413
image: mongo:3.2
514
container_name: 'mongodb'

server-mongo/.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
Tests/

server-mongo/Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM node:10.16.0
2+
WORKDIR /usr/src/app
3+
4+
COPY . .
5+
6+
RUN npm install
7+
8+
EXPOSE 8000
9+
CMD ["npm", "start"]

server-mongo/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## To start mongo with docker:
22

33
> Reference: https://www.thepolyglotdeveloper.com/2019/01/getting-started-mongodb-docker-container-deployment/
4+
> Reference: https://dev.to/jay97/docker-compose-an-express-and-mongo-app-aai
5+
> Refereence: https://nodejs.org/de/docs/guides/nodejs-docker-webapp/
46
57
```bash
68
docker-compose up -d

server-mongo/app.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const express = require('express'),
33
app = express(),
44
port = process.env.PORT || 3000,
55
mongoose = require('mongoose'),
6-
bodyParser = require('body-parser');
6+
bodyParser = require('body-parser')
7+
dotenv = require('dotenv');
8+
dotenv.config();
79

810
// mongoose.connect('mongodb://localhost/bookAPI')
911

@@ -34,9 +36,11 @@ let db;
3436
mongoose.Promise = global.Promise;
3537

3638
if(process.env.ENV === 'Test') {
37-
db = mongoose.connect('mongodb://localhost/bookAPI_test'); // Our database is going to connect here in tests environment
39+
// db = mongoose.connect('mongodb://localhost/bookAPI_test'); // Our database is going to connect here in tests environment
40+
db = mongoose.connect(`mongodb://${process.env.ENVIRONMENT}/bookAPI_test`); // Our database is going to connect here in tests environment
3841
} else {
39-
db = mongoose.connect('mongodb://localhost/bookAPI');
42+
// db = mongoose.connect('mongodb://localhost/bookAPI'); // Local
43+
db = mongoose.connect(`mongodb://${process.env.ENVIRONMENT}/bookAPI`); // docker
4044
}
4145

4246
const Book = require('./models/bookModel'); // We get the mongoose schema to work with it.

server-mongo/package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server-mongo/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"body-parser": "^1.15.2",
1414
"cors": "^2.8.1",
15+
"dotenv": "^8.0.0",
1516
"express": "^4.14.0",
1617
"gulp": "~3.9.1",
1718
"gulp-mocha": "~3.0.1",

0 commit comments

Comments
 (0)