appinit-templates 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/base/common/,gitignore.ejs +33 -0
- package/base/common/.prettierrc.ejs +5 -0
- package/base/common/db.ejs +33 -0
- package/base/common/env.ejs +4 -0
- package/base/common/src/middleware/asyncHandler.ejs +5 -0
- package/base/common/src/routes/health.routes.ejs +12 -0
- package/base/common/src/routes/index.ejs +12 -0
- package/base/common/src/utils/ApiError.ejs +9 -0
- package/base/common/src/utils/logger.ejs +25 -0
- package/base/javascript/.prettierrc.ejs +1 -0
- package/base/javascript/Readme.md.ejs +135 -0
- package/base/javascript/eslint.config.js.ejs +26 -0
- package/base/javascript/src/config/db.js.ejs +34 -0
- package/base/javascript/src/config/env.js.ejs +37 -0
- package/base/javascript/src/controllers/health.controller.js.ejs +40 -0
- package/base/javascript/src/index.js.ejs +54 -0
- package/base/javascript/src/middleware/asyncHandler.js.ejs +1 -0
- package/base/javascript/src/middleware/errorHandler.js.ejs +11 -0
- package/base/javascript/src/routes/health.routes.js.ejs +1 -0
- package/base/javascript/src/routes/index.js.ejs +7 -0
- package/base/javascript/src/utils/ApiError.js.ejs +1 -0
- package/base/javascript/src/utils/logger.js.ejs +1 -0
- package/base/typescript/.prettierrc.ejs +1 -0
- package/base/typescript/Readme.md.ejs +141 -0
- package/base/typescript/eslint.config.js.ejs +29 -0
- package/base/typescript/src/config/db.ts.ejs +38 -0
- package/base/typescript/src/config/env.ts.ejs +37 -0
- package/base/typescript/src/controllers/health.controller.ts.ejs +50 -0
- package/base/typescript/src/index.ts.ejs +54 -0
- package/base/typescript/src/middleware/errorHandler.ts.ejs +20 -0
- package/base/typescript/src/routes/health.routes.ts.ejs +1 -0
- package/base/typescript/src/routes/index.ts.ejs +7 -0
- package/base/typescript/src/utils/ApiError.ts.ejs +12 -0
- package/base/typescript/src/utils/logger.ts.ejs +1 -0
- package/base/websockets/socket.io/javascript/index.js.ejs +87 -0
- package/base/websockets/socket.io/typescript/index.ts.ejs +81 -0
- package/base/websockets/ws/javascript/index.js.ejs +86 -0
- package/base/websockets/ws/typescript/index.ts.ejs +88 -0
- package/features/docker/base/javascript/Dockerfile.ejs +19 -0
- package/features/docker/base/typescript/Dockerfile.ejs +41 -0
- package/package.json +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dhruvin
|
|
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.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Dependencies
|
|
2
|
+
node_modules/
|
|
3
|
+
jspm_packages/
|
|
4
|
+
|
|
5
|
+
# Debug logs
|
|
6
|
+
npm-debug.log*
|
|
7
|
+
yarn-debug.log*
|
|
8
|
+
yarn-error.log*
|
|
9
|
+
|
|
10
|
+
# Environment variables (CRITICAL)
|
|
11
|
+
.env
|
|
12
|
+
.env.local
|
|
13
|
+
.env.test
|
|
14
|
+
|
|
15
|
+
# Build outputs
|
|
16
|
+
dist/
|
|
17
|
+
build/
|
|
18
|
+
out/
|
|
19
|
+
.next/
|
|
20
|
+
|
|
21
|
+
# IDEs
|
|
22
|
+
.vscode/
|
|
23
|
+
.idea/
|
|
24
|
+
*.swp
|
|
25
|
+
|
|
26
|
+
# OS files
|
|
27
|
+
.DS_Store
|
|
28
|
+
Thumbs.db
|
|
29
|
+
|
|
30
|
+
<%_ if (database === 'postgresql_prisma') { _%>
|
|
31
|
+
# Prisma generated client
|
|
32
|
+
src/generated/prisma
|
|
33
|
+
<%_ } _%>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<%_ if (database === 'mongo') { _%>
|
|
2
|
+
import mongoose from 'mongoose';
|
|
3
|
+
|
|
4
|
+
const connectDb = async () => {
|
|
5
|
+
try {
|
|
6
|
+
if (mongoose.connection.readyState >= 1) return;
|
|
7
|
+
const conn = await mongoose.connect(process.env.MONGODB_URI);
|
|
8
|
+
console.log(`MongoDB Connected: ${conn.connection.host}`);
|
|
9
|
+
} catch (error) {
|
|
10
|
+
console.error(`MongoDB Connection Error: ${error instanceof Error ? error.message : error}`);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default connectDb;
|
|
16
|
+
|
|
17
|
+
<%_ } else if (database === 'postgresql_prisma') { _%>
|
|
18
|
+
import { PrismaClient } from '@prisma/client';
|
|
19
|
+
|
|
20
|
+
// Singleton to prevent connection exhaustion in dev
|
|
21
|
+
const prisma = global.prisma || new PrismaClient();
|
|
22
|
+
|
|
23
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
24
|
+
global.prisma = prisma;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const connectDb = async () => {
|
|
28
|
+
await prisma.$connect();
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { prisma, connectDb };
|
|
32
|
+
export default prisma;
|
|
33
|
+
<%_ } _%>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
<%_ if (language === "ts") { _%>
|
|
3
|
+
import { getHealth } from "../controllers/health.controller";
|
|
4
|
+
<%_ } else { _%>
|
|
5
|
+
import { getHealth } from "../controllers/health.controller.js";
|
|
6
|
+
<%_ } _%>
|
|
7
|
+
|
|
8
|
+
const router = Router();
|
|
9
|
+
|
|
10
|
+
router.get("/", getHealth);
|
|
11
|
+
|
|
12
|
+
export default router;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
<%_ if (language === "ts") { _%>
|
|
3
|
+
import healthRoutes from "./health.routes";
|
|
4
|
+
<%_ } else { _%>
|
|
5
|
+
import healthRoutes from "./health.routes.js";
|
|
6
|
+
<%_ } _%>
|
|
7
|
+
|
|
8
|
+
const router = Router();
|
|
9
|
+
|
|
10
|
+
router.use("/health", healthRoutes);
|
|
11
|
+
|
|
12
|
+
export default router;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import pino from "pino";
|
|
2
|
+
<%_ if (language === "js") { _%>
|
|
3
|
+
import { config } from "../config/env.js";
|
|
4
|
+
<%_ } else { _%>
|
|
5
|
+
import { config } from "../config/env";
|
|
6
|
+
<%_ } _%>
|
|
7
|
+
|
|
8
|
+
const logger =
|
|
9
|
+
config.nodeEnv === "development"
|
|
10
|
+
? pino(
|
|
11
|
+
{ level: "debug" },
|
|
12
|
+
pino.transport({
|
|
13
|
+
target: "pino-pretty",
|
|
14
|
+
options: {
|
|
15
|
+
colorize: true,
|
|
16
|
+
ignore: "pid,hostname",
|
|
17
|
+
translateTime: "SYS:standard",
|
|
18
|
+
},
|
|
19
|
+
}),
|
|
20
|
+
)
|
|
21
|
+
: pino({ level: "info" });
|
|
22
|
+
|
|
23
|
+
export { logger };
|
|
24
|
+
|
|
25
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%- await include("../common/.prettierrc.ejs") %>
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# <%= name %>
|
|
2
|
+
|
|
3
|
+
Production-ready Node.js backend starter generated by AppInit.
|
|
4
|
+
|
|
5
|
+
## Template
|
|
6
|
+
<%_ if (template.includes("websocket")) { _%>
|
|
7
|
+
- Type: WebSocket + REST API
|
|
8
|
+
- WebSocket engine: <%= websocket_package %>
|
|
9
|
+
<%_ } else { _%>
|
|
10
|
+
- Type: REST API
|
|
11
|
+
<%_ } _%>
|
|
12
|
+
- Language: JavaScript (ESM)
|
|
13
|
+
<%_ if (database === "mongo") { _%>
|
|
14
|
+
- Database: MongoDB (Mongoose)
|
|
15
|
+
<%_ } else if (database === "postgresql_prisma") { _%>
|
|
16
|
+
- Database: PostgreSQL (Prisma)
|
|
17
|
+
<%_ } else { _%>
|
|
18
|
+
- Database: None
|
|
19
|
+
<%_ } _%>
|
|
20
|
+
|
|
21
|
+
## Included Packages
|
|
22
|
+
- express
|
|
23
|
+
- dotenv
|
|
24
|
+
- cors
|
|
25
|
+
- helmet
|
|
26
|
+
- jsonwebtoken
|
|
27
|
+
- zod
|
|
28
|
+
- pino
|
|
29
|
+
- pino-http
|
|
30
|
+
- pino-pretty
|
|
31
|
+
<%_ if (template.includes("websocket")) { _%>
|
|
32
|
+
- <%= websocket_package %>
|
|
33
|
+
<%_ } _%>
|
|
34
|
+
<%_ if (database === "mongo") { _%>
|
|
35
|
+
- mongoose
|
|
36
|
+
<%_ } else if (database === "postgresql_prisma") { _%>
|
|
37
|
+
- @prisma/client
|
|
38
|
+
- prisma
|
|
39
|
+
<%_ } _%>
|
|
40
|
+
- nodemon
|
|
41
|
+
- eslint
|
|
42
|
+
- prettier
|
|
43
|
+
|
|
44
|
+
## Boilerplate Structure
|
|
45
|
+
```txt
|
|
46
|
+
.
|
|
47
|
+
|-- .env
|
|
48
|
+
|-- .gitignore
|
|
49
|
+
|-- eslint.config.js
|
|
50
|
+
<%_ if (Dockerfile) { _%>
|
|
51
|
+
|-- Dockerfile
|
|
52
|
+
<%_ } _%>
|
|
53
|
+
|-- index.js
|
|
54
|
+
|-- package.json
|
|
55
|
+
|-- src
|
|
56
|
+
| |-- config
|
|
57
|
+
| | |-- env.js
|
|
58
|
+
| | `-- db.js
|
|
59
|
+
| |-- controllers
|
|
60
|
+
| | `-- health.controller.js
|
|
61
|
+
| |-- middleware
|
|
62
|
+
| | |-- asyncHandler.js
|
|
63
|
+
| | `-- errorHandler.js
|
|
64
|
+
| |-- routes
|
|
65
|
+
| | |-- index.js
|
|
66
|
+
| | `-- health.routes.js
|
|
67
|
+
| `-- utils
|
|
68
|
+
| |-- ApiError.js
|
|
69
|
+
| `-- logger.js
|
|
70
|
+
`-- Readme
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Quick Start
|
|
74
|
+
```bash
|
|
75
|
+
npm run dev
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Server default: `http://localhost:3000`
|
|
79
|
+
Health route: `GET /api/health`
|
|
80
|
+
|
|
81
|
+
## Environment Variables
|
|
82
|
+
Base variables:
|
|
83
|
+
- `PORT`
|
|
84
|
+
- `NODE_ENV`
|
|
85
|
+
- `JWT_SECRET`
|
|
86
|
+
- `JWT_EXPIRES_IN`
|
|
87
|
+
<%_ if (database === "mongo") { _%>
|
|
88
|
+
- `MONGODB_URI` (required for MongoDB)
|
|
89
|
+
<%_ } else if (database === "postgresql_prisma") { _%>
|
|
90
|
+
- `DATABASE_URL` (required for PostgreSQL + Prisma)
|
|
91
|
+
<%_ } _%>
|
|
92
|
+
|
|
93
|
+
Update `.env` before running in production.
|
|
94
|
+
|
|
95
|
+
## Database Notes
|
|
96
|
+
<%_ if (database === "mongo") { _%>
|
|
97
|
+
- `src/config/db.js` includes a `connectDb` helper using Mongoose.
|
|
98
|
+
- Add your Mongo connection string in `.env` as `MONGODB_URI`.
|
|
99
|
+
- Call `connectDb()` during app bootstrap if your flow requires explicit DB connection startup.
|
|
100
|
+
<%_ } else if (database === "postgresql_prisma") { _%>
|
|
101
|
+
- Prisma is initialized when the project is generated.
|
|
102
|
+
- Set `DATABASE_URL` in `.env`.
|
|
103
|
+
- Generate/apply schema as needed:
|
|
104
|
+
```bash
|
|
105
|
+
npx prisma generate
|
|
106
|
+
npx prisma migrate dev
|
|
107
|
+
```
|
|
108
|
+
<%_ } else { _%>
|
|
109
|
+
- No database package is enabled.
|
|
110
|
+
- You can add one later by installing your preferred client/ORM and updating `src/config/db.js`.
|
|
111
|
+
<%_ } _%>
|
|
112
|
+
|
|
113
|
+
<%_ if (template.includes("websocket")) { _%>
|
|
114
|
+
## WebSocket Notes
|
|
115
|
+
- HTTP and WebSocket share the same server instance.
|
|
116
|
+
<%_ if (websocket_package === "socket.io") { _%>
|
|
117
|
+
- Socket.io server is configured in `index.js`.
|
|
118
|
+
- Starter event flow includes a ping/pong example.
|
|
119
|
+
- Use Socket.io client for browser/app connections.
|
|
120
|
+
<%_ } else { _%>
|
|
121
|
+
- `ws` server is configured in `index.js`.
|
|
122
|
+
- Messages are expected in JSON format.
|
|
123
|
+
- Starter flow includes ping handling and basic socket error/close behavior.
|
|
124
|
+
<%_ } _%>
|
|
125
|
+
<%_ } _%>
|
|
126
|
+
|
|
127
|
+
<%_ if (Dockerfile) { _%>
|
|
128
|
+
## Dockerfile
|
|
129
|
+
- A Dockerfile is generated in the project root.
|
|
130
|
+
- Basic usage:
|
|
131
|
+
```bash
|
|
132
|
+
docker build -t <%= name %> .
|
|
133
|
+
docker run --env-file .env -p 3000:3000 <%= name %>
|
|
134
|
+
```
|
|
135
|
+
<%_ } _%>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import js from "@eslint/js";
|
|
2
|
+
|
|
3
|
+
export default [
|
|
4
|
+
js.configs.recommended,
|
|
5
|
+
{
|
|
6
|
+
files: ["**/*.js"],
|
|
7
|
+
languageOptions: {
|
|
8
|
+
ecmaVersion: 2022,
|
|
9
|
+
sourceType: "module",
|
|
10
|
+
globals: {
|
|
11
|
+
console: "readonly",
|
|
12
|
+
process: "readonly",
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
rules: {
|
|
16
|
+
"no-console": "off",
|
|
17
|
+
"no-unused-vars": [
|
|
18
|
+
"warn",
|
|
19
|
+
{
|
|
20
|
+
argsIgnorePattern: "^_",
|
|
21
|
+
varsIgnorePattern: "^_",
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
];
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<%_ if (database === 'mongo') { _%>
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
const connectDb = async () => {
|
|
5
|
+
try {
|
|
6
|
+
if (mongoose.connection.readyState >= 1) return;
|
|
7
|
+
const conn = await mongoose.connect(process.env.MONGODB_URI);
|
|
8
|
+
console.log(`MongoDB Connected: ${conn.connection.host}`);
|
|
9
|
+
} catch (error) {
|
|
10
|
+
console.error(
|
|
11
|
+
`MongoDB Connection Error: ${error instanceof Error ? error.message : error}`
|
|
12
|
+
);
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default connectDb;
|
|
18
|
+
<%_ } else if (database === 'postgresql_prisma') { _%>
|
|
19
|
+
import { PrismaClient } from "@prisma/client";
|
|
20
|
+
|
|
21
|
+
const prisma = global.prisma || new PrismaClient();
|
|
22
|
+
|
|
23
|
+
if (process.env.NODE_ENV !== "production") {
|
|
24
|
+
global.prisma = prisma;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const connectDb = async () => {
|
|
28
|
+
await prisma.$connect();
|
|
29
|
+
console.log("PostgreSQL Connected");
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { prisma, connectDb };
|
|
33
|
+
export default prisma;
|
|
34
|
+
<%_ } _%>
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import dotenv from "dotenv";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
dotenv.config();
|
|
5
|
+
|
|
6
|
+
const envSchema = z.object({
|
|
7
|
+
NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
|
|
8
|
+
PORT: z.coerce.number().default(3000),
|
|
9
|
+
<%_ if (database === 'mongo') { _%>
|
|
10
|
+
MONGODB_URI: z.string().min(1, "MongoDB URI is required"),
|
|
11
|
+
<%_ } else if (database === 'postgresql_prisma') { _%>
|
|
12
|
+
DATABASE_URL: z.string().min(1, "Database URL is required"),
|
|
13
|
+
<%_ } _%>
|
|
14
|
+
JWT_SECRET: z.string().min(1, "JWT secret is required"),
|
|
15
|
+
JWT_EXPIRES_IN: z.string().default("7d"),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const parsed = envSchema.safeParse(process.env);
|
|
19
|
+
|
|
20
|
+
if (!parsed.success) {
|
|
21
|
+
console.error("Invalid environment variables:", parsed.error.format());
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const config = {
|
|
26
|
+
nodeEnv: parsed.data.NODE_ENV,
|
|
27
|
+
port: parsed.data.PORT,
|
|
28
|
+
<%_ if (database === 'mongo') { _%>
|
|
29
|
+
mongoUri: parsed.data.MONGODB_URI,
|
|
30
|
+
<%_ } else if (database === 'postgresql_prisma') { _%>
|
|
31
|
+
databaseUrl: parsed.data.DATABASE_URL,
|
|
32
|
+
<%_ } _%>
|
|
33
|
+
jwt: {
|
|
34
|
+
secret: parsed.data.JWT_SECRET,
|
|
35
|
+
expiresIn: parsed.data.JWT_EXPIRES_IN,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { asyncHandler } from "../middleware/asyncHandler.js";
|
|
2
|
+
<%_ if (database === 'mongo') { _%>
|
|
3
|
+
import mongoose from "mongoose";
|
|
4
|
+
<%_ } else if (database === 'postgresql_prisma') { _%>
|
|
5
|
+
import prisma from "../config/db.js";
|
|
6
|
+
<%_ } _%>
|
|
7
|
+
|
|
8
|
+
export const getHealth = asyncHandler(async (req, res) => {
|
|
9
|
+
const uptime = process.uptime();
|
|
10
|
+
<%_ if (database === 'none') { _%>
|
|
11
|
+
const dbStatus = "disconnected";
|
|
12
|
+
const isHealthy = true;
|
|
13
|
+
<%_ } else { _%>
|
|
14
|
+
let dbStatus = "disconnected";
|
|
15
|
+
|
|
16
|
+
<%_ if (database === 'mongo') { _%>
|
|
17
|
+
dbStatus = mongoose.connection.readyState === 1 ? "connected" : "disconnected";
|
|
18
|
+
|
|
19
|
+
<%_ } else if (database === 'postgresql_prisma') { _%>
|
|
20
|
+
try {
|
|
21
|
+
await prisma.$queryRaw`SELECT 1`;
|
|
22
|
+
dbStatus = "connected";
|
|
23
|
+
} catch (e) {
|
|
24
|
+
dbStatus = "error";
|
|
25
|
+
}
|
|
26
|
+
<%_ } _%>
|
|
27
|
+
|
|
28
|
+
const isHealthy = dbStatus === "connected";
|
|
29
|
+
<%_ } _%>
|
|
30
|
+
|
|
31
|
+
res.status(isHealthy ? 200 : 503).json({
|
|
32
|
+
success: true,
|
|
33
|
+
data: {
|
|
34
|
+
status: "ok",
|
|
35
|
+
timestamp: new Date().toISOString(),
|
|
36
|
+
uptime: `${Math.floor(uptime)}s`,
|
|
37
|
+
database: dbStatus,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// javascript index file
|
|
2
|
+
import express from "express";
|
|
3
|
+
import helmet from "helmet";
|
|
4
|
+
import cors from "cors";
|
|
5
|
+
import routes from "./src/routes/index.js";
|
|
6
|
+
import errorHandler from "./src/middleware/errorHandler.js";
|
|
7
|
+
import { logger } from "./src/utils/logger.js";
|
|
8
|
+
import { config } from "./src/config/env.js";
|
|
9
|
+
|
|
10
|
+
<%_ if (database === 'postgresql_prisma') { _%>
|
|
11
|
+
// Prisma note: in prisma/schema.prisma use:
|
|
12
|
+
// provider = "prisma-client-js"
|
|
13
|
+
// output = "../generated/prisma"
|
|
14
|
+
<%_ } _%>
|
|
15
|
+
|
|
16
|
+
const app = express();
|
|
17
|
+
const port = config.port;
|
|
18
|
+
|
|
19
|
+
app.use(express.json());
|
|
20
|
+
app.use(helmet());
|
|
21
|
+
app.use(cors());
|
|
22
|
+
app.use(express.urlencoded({ extended: true }));
|
|
23
|
+
|
|
24
|
+
// Call connectDb() from ./src/config/db.js to connect to your database and also update the env vars with database url
|
|
25
|
+
|
|
26
|
+
// logger middleware
|
|
27
|
+
app.use((req, res, next) => {
|
|
28
|
+
logger.info({
|
|
29
|
+
method: req.method,
|
|
30
|
+
path: req.path,
|
|
31
|
+
ip: req.ip,
|
|
32
|
+
});
|
|
33
|
+
next();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
app.use("/api", routes);
|
|
37
|
+
|
|
38
|
+
app.use(errorHandler);
|
|
39
|
+
|
|
40
|
+
app.listen(port, () => {
|
|
41
|
+
logger.info(`Server running on port ${port}`);
|
|
42
|
+
logger.info(`Environment: ${config.nodeEnv}`);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
process.on("SIGINT", () => {
|
|
46
|
+
console.log("SIGINT received, forcefully stopping the server");
|
|
47
|
+
process.exit(0);
|
|
48
|
+
});
|
|
49
|
+
process.on("SIGTERM", () => {
|
|
50
|
+
console.log("SIGTERM received, gracefully stopping the server");
|
|
51
|
+
process.exit(0);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export default app;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%- await include("../../../common/src/middleware/asyncHandler.ejs") %>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const errorHandler = (err,req,res,next)=>{
|
|
2
|
+
const statusCode = err.statusCode || 500;
|
|
3
|
+
const message = err.message || "Internal Server Error";
|
|
4
|
+
|
|
5
|
+
res.status(statusCode).json({
|
|
6
|
+
success: false,
|
|
7
|
+
message: message,
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default errorHandler;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%- await include("../../../common/src/routes/health.routes.ejs") %>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%- await include("../../../common/src/utils/ApiError.ejs") %>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%- await include("../../../common/src/utils/logger.ejs") %>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%- await include("../common/.prettierrc.ejs") %>
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# <%= name %>
|
|
2
|
+
|
|
3
|
+
Production-ready Node.js backend starter generated by AppInit.
|
|
4
|
+
|
|
5
|
+
## Template
|
|
6
|
+
<%_ if (template.includes("websocket")) { _%>
|
|
7
|
+
- Type: WebSocket + REST API
|
|
8
|
+
- WebSocket engine: <%= websocket_package %>
|
|
9
|
+
<%_ } else { _%>
|
|
10
|
+
- Type: REST API
|
|
11
|
+
<%_ } _%>
|
|
12
|
+
- Language: TypeScript
|
|
13
|
+
<%_ if (database === "mongo") { _%>
|
|
14
|
+
- Database: MongoDB (Mongoose)
|
|
15
|
+
<%_ } else if (database === "postgresql_prisma") { _%>
|
|
16
|
+
- Database: PostgreSQL (Prisma)
|
|
17
|
+
<%_ } else { _%>
|
|
18
|
+
- Database: None
|
|
19
|
+
<%_ } _%>
|
|
20
|
+
|
|
21
|
+
## Included Packages
|
|
22
|
+
- express
|
|
23
|
+
- dotenv
|
|
24
|
+
- cors
|
|
25
|
+
- helmet
|
|
26
|
+
- jsonwebtoken
|
|
27
|
+
- zod
|
|
28
|
+
- pino
|
|
29
|
+
- pino-http
|
|
30
|
+
- pino-pretty
|
|
31
|
+
<%_ if (template.includes("websocket")) { _%>
|
|
32
|
+
- <%= websocket_package %>
|
|
33
|
+
<%_ } _%>
|
|
34
|
+
<%_ if (database === "mongo") { _%>
|
|
35
|
+
- mongoose
|
|
36
|
+
<%_ } else if (database === "postgresql_prisma") { _%>
|
|
37
|
+
- @prisma/client
|
|
38
|
+
- prisma
|
|
39
|
+
<%_ } _%>
|
|
40
|
+
- typescript
|
|
41
|
+
- ts-node
|
|
42
|
+
- nodemon
|
|
43
|
+
- eslint
|
|
44
|
+
- prettier
|
|
45
|
+
|
|
46
|
+
## Boilerplate Structure
|
|
47
|
+
```txt
|
|
48
|
+
.
|
|
49
|
+
|-- .env
|
|
50
|
+
|-- .gitignore
|
|
51
|
+
|-- eslint.config.js
|
|
52
|
+
<%_ if (Dockerfile) { _%>
|
|
53
|
+
|-- Dockerfile
|
|
54
|
+
<%_ } _%>
|
|
55
|
+
|-- package.json
|
|
56
|
+
|-- tsconfig.json
|
|
57
|
+
|-- src
|
|
58
|
+
| |-- index.ts
|
|
59
|
+
| |-- config
|
|
60
|
+
| | |-- env.ts
|
|
61
|
+
| | `-- db.ts
|
|
62
|
+
| |-- controllers
|
|
63
|
+
| | `-- health.controller.ts
|
|
64
|
+
| |-- middleware
|
|
65
|
+
| | `-- errorHandler.ts
|
|
66
|
+
| |-- routes
|
|
67
|
+
| | |-- index.ts
|
|
68
|
+
| | `-- health.routes.ts
|
|
69
|
+
| `-- utils
|
|
70
|
+
| |-- ApiError.ts
|
|
71
|
+
| `-- logger.ts
|
|
72
|
+
`-- Readme
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
```bash
|
|
77
|
+
npm run dev
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Server default: `http://localhost:3000`
|
|
81
|
+
Health route: `GET /api/health`
|
|
82
|
+
|
|
83
|
+
## Environment Variables
|
|
84
|
+
Base variables:
|
|
85
|
+
- `PORT`
|
|
86
|
+
- `NODE_ENV`
|
|
87
|
+
- `JWT_SECRET`
|
|
88
|
+
- `JWT_EXPIRES_IN`
|
|
89
|
+
<%_ if (database === "mongo") { _%>
|
|
90
|
+
- `MONGODB_URI` (required for MongoDB)
|
|
91
|
+
<%_ } else if (database === "postgresql_prisma") { _%>
|
|
92
|
+
- `DATABASE_URL` (required for PostgreSQL + Prisma)
|
|
93
|
+
<%_ } _%>
|
|
94
|
+
|
|
95
|
+
Update `.env` before running in production.
|
|
96
|
+
|
|
97
|
+
## Database Notes
|
|
98
|
+
<%_ if (database === "mongo") { _%>
|
|
99
|
+
- `src/config/db.ts` includes a `connectDb` helper using Mongoose.
|
|
100
|
+
- Add your Mongo connection string in `.env` as `MONGODB_URI`.
|
|
101
|
+
- Call `connectDb()` during app bootstrap if your flow requires explicit DB connection startup.
|
|
102
|
+
<%_ } else if (database === "postgresql_prisma") { _%>
|
|
103
|
+
- Prisma is initialized when the project is generated.
|
|
104
|
+
- Set `DATABASE_URL` in `.env`.
|
|
105
|
+
- Generate/apply schema as needed:
|
|
106
|
+
```bash
|
|
107
|
+
npx prisma generate
|
|
108
|
+
npx prisma migrate dev
|
|
109
|
+
```
|
|
110
|
+
<%_ } else { _%>
|
|
111
|
+
- No database package is enabled.
|
|
112
|
+
- You can add one later by installing your preferred client/ORM and updating `src/config/db.ts`.
|
|
113
|
+
<%_ } _%>
|
|
114
|
+
|
|
115
|
+
<%_ if (template.includes("websocket")) { _%>
|
|
116
|
+
## WebSocket Notes
|
|
117
|
+
- HTTP and WebSocket share the same server instance.
|
|
118
|
+
<%_ if (websocket_package === "socket.io") { _%>
|
|
119
|
+
- Socket.io server is configured in `src/index.ts`.
|
|
120
|
+
- Starter event flow includes a ping/pong example.
|
|
121
|
+
- Use Socket.io client for browser/app connections.
|
|
122
|
+
<%_ } else { _%>
|
|
123
|
+
- `ws` server is configured in `src/index.ts`.
|
|
124
|
+
- Messages are expected in JSON format.
|
|
125
|
+
- Starter flow includes ping handling and basic socket error/close behavior.
|
|
126
|
+
<%_ } _%>
|
|
127
|
+
<%_ } _%>
|
|
128
|
+
|
|
129
|
+
## Scripts
|
|
130
|
+
- `npm run dev`: Run in development mode with `ts-node`.
|
|
131
|
+
- `npm run build`: Build TypeScript output.
|
|
132
|
+
|
|
133
|
+
<%_ if (Dockerfile) { _%>
|
|
134
|
+
## Dockerfile
|
|
135
|
+
- A Dockerfile is generated in the project root.
|
|
136
|
+
- Basic usage:
|
|
137
|
+
```bash
|
|
138
|
+
docker build -t <%= name %> .
|
|
139
|
+
docker run --env-file .env -p 3000:3000 <%= name %>
|
|
140
|
+
```
|
|
141
|
+
<%_ } _%>
|