Skip to content

Commit d2e464d

Browse files
publish open-source Node.js backend starter template
1 parent 900c0a3 commit d2e464d

79 files changed

Lines changed: 33801 additions & 0 deletions

Some content is hidden

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

‎.dockerignore‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Dependencies
2+
node_modules/
3+
npm-debug.log*
4+
yarn-debug.log*
5+
yarn-error.log*
6+
package-lock.json.backup
7+
8+
# Environment files
9+
.env
10+
.env.local
11+
.env.*.local
12+
.env.development
13+
.env.test
14+
.env.production
15+
16+
# Logs
17+
logs/
18+
*.log
19+
npm-debug.log*
20+
yarn-debug.log*
21+
yarn-error.log*
22+
pm2-*.log
23+
24+
# OS files
25+
.DS_Store
26+
Thumbs.db
27+
*.swp
28+
*.swo
29+
*~
30+
31+
# IDE
32+
.vscode/
33+
.idea/
34+
*.sublime-*
35+
36+
# Git
37+
.git/
38+
.gitignore
39+
.gitattributes
40+
41+
# Testing
42+
coverage/
43+
.nyc_output/
44+
*.test.js
45+
*.spec.js
46+
__tests__/
47+
test/
48+
49+
# Documentation
50+
*.md
51+
docs/
52+
README.md
53+
QUICKSTART.md
54+
PROJECT_STRUCTURE.md
55+
56+
# Docker
57+
Dockerfile*
58+
docker-compose*.yml
59+
.dockerignore
60+
61+
# CI/CD
62+
.github/
63+
.gitlab-ci.yml
64+
.travis.yml
65+
.circleci/
66+
67+
# Build files
68+
dist/
69+
build/
70+
*.map
71+
72+
# Credentials - Allow credentials folder for Google Cloud Storage
73+
# Note: In production, use environment variables or mounted volumes instead
74+
# Block JSON files first
75+
**/*.json
76+
# Then allow specific exceptions (order matters - negations must come after)
77+
!package*.json
78+
!credentials/**/*.json
79+
!credentials/google-backend-master.json
80+
# Allow credentials folder
81+
!credentials/
82+
**/*.pem
83+
**/*.key
84+
**/*.crt
85+
**/*.plist
86+
87+
# Test files
88+
test-prisma-connection.js
89+
**/*.test.js
90+
**/*.spec.js
91+
__tests__/
92+
test/
93+
94+
# Temporary files
95+
tmp/
96+
temp/
97+
*.tmp
98+
99+
# Cache
100+
.cache/
101+
.npm/
102+
.yarn/
103+
104+
# Misc
105+
.editorconfig
106+
.eslintrc*
107+
.prettierrc*
108+

‎README.md‎

Lines changed: 1064 additions & 0 deletions
Large diffs are not rendered by default.

‎api/Dockerfile‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# ==============================================================================
2+
# Multi-stage Dockerfile for Node.js Backend
3+
# Optimized for production with security and performance best practices
4+
# ==============================================================================
5+
6+
# ==============================================================================
7+
# Stage 1: Dependencies - Install and cache dependencies
8+
# ==============================================================================
9+
FROM node:20-alpine AS dependencies
10+
11+
WORKDIR /api
12+
13+
RUN apk add --no-cache \
14+
python3 \
15+
make \
16+
g++ \
17+
curl \
18+
&& rm -rf /var/cache/apk/*
19+
20+
COPY package*.json ./
21+
COPY prisma.config.ts ./
22+
COPY prisma/schema.prisma ./prisma/
23+
COPY credentials/google-backend-master.json ./credentials/
24+
25+
RUN npm ci --prefer-offline --no-audit --progress=false && \
26+
npm cache clean --force
27+
28+
# ==============================================================================
29+
# Stage 2: Builder - Generate Prisma Client and prepare production build
30+
# ==============================================================================
31+
FROM node:20-alpine AS builder
32+
33+
WORKDIR /api
34+
35+
COPY --from=dependencies /api/node_modules ./node_modules
36+
37+
COPY prisma.config.ts ./
38+
COPY prisma/ ./prisma/
39+
COPY credentials/ ./credentials/
40+
41+
RUN DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy" npx prisma generate
42+
43+
COPY package*.json ./
44+
45+
RUN npm prune --production && \
46+
npm cache clean --force
47+
48+
# ==============================================================================
49+
# Stage 3: Production - Final optimized image
50+
# ==============================================================================
51+
FROM node:20-alpine AS production
52+
53+
LABEL maintainer="Node.js Backend Team" \
54+
version="1.0.0" \
55+
description="Node.js Backend API - Express Version" \
56+
org.opencontainers.image.title="nodejs-backend-with-postgresql" \
57+
org.opencontainers.image.description="Production-ready Node.js/Express backend API" \
58+
org.opencontainers.image.version="1.0.0"
59+
60+
RUN addgroup -g 1001 -S mrdas && \
61+
adduser -S mrdas -u 1001 -G mrdas
62+
63+
WORKDIR /api
64+
65+
RUN apk add --no-cache \
66+
curl \
67+
dumb-init \
68+
&& rm -rf /var/cache/apk/*
69+
70+
COPY --from=builder --chown=mrdas:mrdas /api/node_modules ./node_modules
71+
72+
COPY --from=builder --chown=mrdas:mrdas /api/package*.json ./
73+
74+
COPY --from=builder --chown=mrdas:mrdas /api/prisma.config.ts ./
75+
COPY --from=builder --chown=mrdas:mrdas /api/prisma ./prisma
76+
COPY --from=builder --chown=mrdas:mrdas /api/credentials ./credentials
77+
78+
COPY --chown=mrdas:mrdas server.js ./
79+
COPY --chown=mrdas:mrdas start.sh ./
80+
COPY --chown=mrdas:mrdas ecosystem.config.js ./
81+
COPY --chown=mrdas:mrdas src ./src
82+
COPY --chown=mrdas:mrdas router ./router
83+
COPY --chown=mrdas:mrdas credentials ./credentials
84+
85+
RUN mkdir -p /api/logs && \
86+
chown -R mrdas:mrdas /api/logs && \
87+
chmod -R 755 /api/logs
88+
89+
RUN chmod +x /api/start.sh
90+
91+
USER mrdas
92+
EXPOSE 3000
93+
94+
# Health check
95+
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
96+
CMD curl -f http://localhost:3000/health || exit 1
97+
98+
99+
ENTRYPOINT ["dumb-init", "--"]
100+
CMD ["/api/start.sh"]

‎api/ecosystem.config.js‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* PM2 Ecosystem Configuration
3+
* Process manager configuration for Node.js application
4+
*/
5+
6+
module.exports = {
7+
apps: [
8+
{
9+
name: 'nodejs-backend-with-postgresql-prod',
10+
script: './server.js',
11+
instances: process.env.PM2_INSTANCES || 'max', // Use 'max' to utilize all CPU cores, or set specific number
12+
exec_mode: 'cluster', // Use cluster mode for better traffic handling
13+
env: {
14+
NODE_ENV: 'production',
15+
PORT: 3000,
16+
API_MODE: 'production',
17+
DEBUG_MODE: 'False'
18+
},
19+
error_file: './logs/errors-server.log',
20+
out_file: './logs/success-server.log',
21+
time: true,
22+
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
23+
merge_logs: true,
24+
autorestart: true,
25+
max_restarts: 10,
26+
min_uptime: '10s',
27+
max_memory_restart: '1G',
28+
watch: false,
29+
ignore_watch: ['node_modules', 'logs', '.git'],
30+
instance_var: 'INSTANCE_ID',
31+
kill_timeout: 5000,
32+
wait_ready: true,
33+
listen_timeout: 10000,
34+
shutdown_with_message: true,
35+
increment_var: 'PORT',
36+
env_production: {
37+
NODE_ENV: 'production',
38+
PORT: 3000
39+
}
40+
},
41+
{
42+
name: 'nodejs-backend-with-postgresql-staging',
43+
script: './server.js',
44+
instances: process.env.PM2_INSTANCES || 2, // Use 2 instances for staging
45+
exec_mode: 'cluster', // Use cluster mode for better traffic handling
46+
env: {
47+
NODE_ENV: 'staging',
48+
PORT: 3000,
49+
API_MODE: 'staging',
50+
DEBUG_MODE: 'False'
51+
},
52+
error_file: './logs/errors-server.log',
53+
out_file: './logs/success-server.log',
54+
time: true,
55+
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
56+
merge_logs: true,
57+
autorestart: true,
58+
max_restarts: 10,
59+
min_uptime: '10s',
60+
max_memory_restart: '1G',
61+
watch: false,
62+
ignore_watch: ['node_modules', 'logs', '.git'],
63+
instance_var: 'INSTANCE_ID',
64+
kill_timeout: 5000,
65+
wait_ready: true,
66+
listen_timeout: 10000
67+
},
68+
{
69+
name: 'nodejs-backend-with-postgresql-dev',
70+
script: './server.js',
71+
instances: process.env.PM2_INSTANCES || 1, // Use 1 instance for development
72+
exec_mode: 'cluster', // Use cluster mode for better traffic handling
73+
env: {
74+
NODE_ENV: 'development',
75+
PORT: 3000,
76+
API_MODE: 'development',
77+
DEBUG_MODE: process.env.DEBUG_MODE || 'false'
78+
},
79+
error_file: './logs/errors-server.log',
80+
out_file: './logs/success-server.log',
81+
time: true,
82+
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
83+
merge_logs: true,
84+
autorestart: true,
85+
max_restarts: 10,
86+
min_uptime: '10s',
87+
max_memory_restart: '1G',
88+
watch: false,
89+
ignore_watch: ['node_modules', 'logs', '.git'],
90+
instance_var: 'INSTANCE_ID',
91+
kill_timeout: 5000,
92+
wait_ready: true,
93+
listen_timeout: 10000
94+
}
95+
]
96+
};
97+

‎api/import.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
while IFS= read -r line; do
4+
if [[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*=.* ]]; then
5+
export "$line"
6+
fi
7+
done < ../.env

0 commit comments

Comments
 (0)