Skip to content

Commit b49faac

Browse files
authored
Merge pull request #3 from mugarate12/feature/add_nginx_and_pm2
Feature/add nginx and pm2
2 parents 98e2131 + ea53c5b commit b49faac

10 files changed

Lines changed: 76 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.2.5] - 2022-10-25
10+
### Added
11+
- Add Nginx and PM2 to production build of docker
12+
913
## [1.1.5] - 2022-10-18
1014
### Added
1115
- Create Docker files to development, test and production builds

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ project
4848
│ package.json
4949
│ tsconfig.json
5050
│ tsconfig.spec.json
51+
│ pm2.json
52+
│ nginx
53+
| │ default.conf
54+
| │ Dockerfile
5155
│ __tests__
5256
│ └───unit
5357
| │ | example.test.ts
@@ -89,6 +93,11 @@ build your project and immediately run your app builded:
8993
npm start
9094
```
9195

96+
run project in production mode with pm2:
97+
```shell
98+
npm run start:pm2
99+
```
100+
92101
run project in development build:
93102
```shell
94103
npm run dev

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "basic-node-ts-config",
3-
"version": "1.1.5",
3+
"version": "1.2.5",
44
"description": "Configuração básica para um ambiente de desenvolvimento com NodeJs + TypeScript",
55
"main": "./lib/copyFile.js",
66
"preferGlobal": true,

project/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ COPY package*.json ./
99

1010
# Install app dependencies
1111
RUN npm i
12+
RUN npm install pm2 -g
1213

1314
# Copy all files from current directory to app directory
1415
COPY . .
@@ -17,4 +18,4 @@ COPY . .
1718
EXPOSE 8000
1819

1920
# Run the app
20-
CMD [ "npm", "start" ]
21+
CMD [ "npm", "run", "start:pm2-runtime" ]

project/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ build your project and immediately run your app builded:
4444
npm start
4545
```
4646

47+
run project in production mode with pm2:
48+
```shell
49+
npm run start:pm2
50+
```
51+
4752
run project in development build:
4853
```shell
4954
npm run dev
@@ -108,12 +113,16 @@ project
108113
│ package.json
109114
│ tsconfig.json
110115
│ tsconfig.spec.json
116+
│ pm2.json
111117
│ __tests__
112118
│ └───unit
113119
| │ | example.test.ts
114120
| │ | redis.test.ts
115121
│ └───integration
116122
| │ | example.test.ts
123+
│ nginx
124+
| │ default.conf
125+
| │ Dockerfile
117126
└───src
118127
| | app.ts
119128
| | server.ts

project/docker-compose.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
version: "3.9"
2-
restart: always
32

43
services:
4+
nginx:
5+
restart: always
6+
build: ./nginx
7+
ports:
8+
- "80:80"
9+
510
redis:
11+
restart: always
612
image: redis
713
container_name: cache
814
expose:
915
- 6379
1016

1117
app:
18+
restart: always
1219
build: ./
1320
volumes:
1421
- ./:/var/www/app
1522
depends_on:
23+
- nginx
1624
- redis
1725
ports:
1826
- "8000:8000"
@@ -21,5 +29,5 @@ services:
2129
- NODE_ENV=production
2230
- PORT=8000
2331
command:
24-
sh -c 'npm start'
32+
sh -c 'npm run start:pm2-runtime'
2533

project/nginx/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM nginx
2+
3+
# Copy the nginx configuration file
4+
COPY default.conf /etc/nginx/conf.d/default.conf

project/nginx/default.conf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
server {
2+
server_name _;
3+
4+
# access_log /var/log/nginx/nodeapp.log;
5+
# error_log /var/log/nginx/nodeapp-error.log error;
6+
7+
location / {
8+
proxy_read_timeout 3600;
9+
proxy_set_header HOST $host;
10+
proxy_set_header X-Real-IP $remote_addr;
11+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12+
proxy_set_header X-Forwarded-Proto $scheme;
13+
14+
proxy_pass http://app:8000; # change the port according to your nodejs port
15+
}
16+
}

project/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"build": "tsc",
88
"dev": "ts-node-dev --poll --respawn --transpile-only --ignore-watch node_modules src/server.ts",
99
"start": "npm run build&&node build/src/server.js",
10+
"start:pm2": "npm run build&&pm2 start pm2.json",
11+
"start:pm2-runtime": "npm run build&&pm2-runtime start pm2.json",
1012
"test": "SET NODE_ENV=test&&jest",
1113
"test:docker": "jest",
1214
"docker:test": "docker-compose -f ./docker-compose.test.yml up",
@@ -16,7 +18,7 @@
1618
"docker:prod": "docker-compose -f ./docker-compose.yml up",
1719
"docker:prod:build": "docker-compose -f ./docker-compose.yml up --build"
1820
},
19-
"keywords": [],
21+
"keywords": ["node", "typescript", "express", "jest", "docker"],
2022
"author": "",
2123
"license": "ISC",
2224
"dependencies": {

project/pm2.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"apps": [
3+
{
4+
"name": "primary",
5+
"instances": 1,
6+
"exec_mode": "cluster",
7+
"script": "./build/src/server.js",
8+
"instance_var": "INSTANCE_ID"
9+
},
10+
{
11+
"name": "replica",
12+
"instances": -1,
13+
"exec_mode": "cluster",
14+
"script": "./build/src/server.js",
15+
"instance_var": "INSTANCE_ID"
16+
}
17+
]
18+
}

0 commit comments

Comments
 (0)