create-server-nodejs 1.0.2 → 1.0.4
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/README.md +90 -3
- package/package.json +1 -1
- package/packages/index.js +187 -31
package/README.md
CHANGED
|
@@ -1,11 +1,98 @@
|
|
|
1
1
|
# create-server-nodejs
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A fast and modern CLI tool to scaffold ready-to-use **Node.js Express** backend projects.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Generate a clean, professional Express server with support for **MongoDB** and **PostgreSQL** in seconds.
|
|
6
6
|
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- ⚡ Extremely fast project generation
|
|
12
|
+
- 📁 Clean and scalable folder structure
|
|
13
|
+
- 🗄️ **MongoDB + Mongoose** (default)
|
|
14
|
+
- 🐘 **PostgreSQL + Prisma** support
|
|
15
|
+
- 🔐 JWT authentication middleware included
|
|
16
|
+
- 📦 Automatic `npm install`
|
|
17
|
+
- 🌐 Install in current directory with `.`
|
|
18
|
+
- ✨ Full ES Modules support (`"type": "module"`)
|
|
19
|
+
- 🚀 Ready for production
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
### Create a new project (MongoDB - Default)
|
|
26
|
+
|
|
27
|
+
```bash
|
|
7
28
|
npx create-server-nodejs my-app
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Create with PostgreSQL
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npx create-server-nodejs my-app --postgresql
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Install in current directory
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npx create-server-nodejs .
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Project Structure
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
my-app/
|
|
47
|
+
├── Controllers/ # Controllers and business logic
|
|
48
|
+
├── Models/ # Database models
|
|
49
|
+
├── Routers/ # API routes
|
|
50
|
+
├── Middlewares/ # Authentication, validation, error handling
|
|
51
|
+
├── Utils/ # Helper functions
|
|
52
|
+
├── config/ # Configuration files
|
|
53
|
+
├── prisma/ # Prisma schema (only when using PostgreSQL)
|
|
54
|
+
├── server.js
|
|
55
|
+
├── app.js
|
|
56
|
+
├── .env.example
|
|
57
|
+
├── package.json
|
|
58
|
+
└── README.md
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## How to Run
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
cd my-app
|
|
65
|
+
|
|
66
|
+
# Copy environment variables
|
|
67
|
+
cp .env.example .env
|
|
68
|
+
|
|
69
|
+
# For PostgreSQL only:
|
|
70
|
+
# npx prisma generate
|
|
71
|
+
# npx prisma db push
|
|
72
|
+
|
|
73
|
+
# Run development server
|
|
74
|
+
npm run dev
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Available Options
|
|
78
|
+
|
|
79
|
+
| Command | Description |
|
|
80
|
+
|---|---|
|
|
81
|
+
| `npx create-server-nodejs <name>` | Create a new project |
|
|
82
|
+
| `npx create-server-nodejs .` | Scaffold in current directory |
|
|
83
|
+
| `--mongodb` | Use MongoDB + Mongoose (default) |
|
|
84
|
+
| `--postgresql` | Use PostgreSQL + Prisma |
|
|
85
|
+
|
|
86
|
+
## Tech Stack
|
|
87
|
+
|
|
88
|
+
- Express.js
|
|
89
|
+
- MongoDB + Mongoose or PostgreSQL + Prisma
|
|
90
|
+
- JWT Authentication
|
|
91
|
+
- bcryptjs, cors, morgan, dotenv
|
|
92
|
+
- Nodemon (dev dependency)
|
|
8
93
|
|
|
9
94
|
## License
|
|
95
|
+
This project is licensed under the MIT License.
|
|
10
96
|
|
|
11
|
-
|
|
97
|
+
Made with ❤️ for faster backend development
|
|
98
|
+
Happy coding!
|
package/package.json
CHANGED
package/packages/index.js
CHANGED
|
@@ -7,21 +7,39 @@ import chalk from 'chalk';
|
|
|
7
7
|
import { execSync } from 'child_process';
|
|
8
8
|
|
|
9
9
|
const __filename = fileURLToPath(import.meta.url);
|
|
10
|
-
const __dirname = path.dirname(__filename);
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
// Get project name or current directory (.)
|
|
12
|
+
let projectName = process.argv[2];
|
|
13
13
|
|
|
14
14
|
if (!projectName) {
|
|
15
15
|
console.log(chalk.red('❌ Please enter project name:'));
|
|
16
|
-
console.log(chalk.yellow(' npx
|
|
16
|
+
console.log(chalk.yellow(' npx create-server-nodejs my-app'));
|
|
17
|
+
console.log(chalk.yellow(' or install in current folder:'));
|
|
18
|
+
console.log(chalk.yellow(' npx create-server-nodejs .'));
|
|
17
19
|
process.exit(1);
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
const
|
|
22
|
+
const args = process.argv.slice(3);
|
|
23
|
+
const useMongo = args.includes('--mongodb') || !args.includes('--postgresql');
|
|
24
|
+
const usePostgres = args.includes('--postgresql');
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
const dbType = usePostgres ? 'postgresql' : 'mongodb';
|
|
27
|
+
const isPostgres = dbType === 'postgresql';
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
// If user entered "." → install in current directory
|
|
30
|
+
const isCurrentDir = projectName === '.';
|
|
31
|
+
const targetDir = isCurrentDir ? process.cwd() : path.join(process.cwd(), projectName);
|
|
32
|
+
|
|
33
|
+
console.log(chalk.blue(`🚀 Creating project: ${isCurrentDir ? 'in current directory' : projectName} (${dbType.toUpperCase()})`));
|
|
34
|
+
|
|
35
|
+
// Create target directory if not installing in current folder
|
|
36
|
+
if (!isCurrentDir) {
|
|
37
|
+
if (fs.existsSync(targetDir)) {
|
|
38
|
+
console.log(chalk.red(`❌ Folder ${projectName} already exists!`));
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
42
|
+
}
|
|
25
43
|
|
|
26
44
|
const folders = [
|
|
27
45
|
'Controllers',
|
|
@@ -34,12 +52,11 @@ const folders = [
|
|
|
34
52
|
|
|
35
53
|
folders.forEach(folder => {
|
|
36
54
|
fs.mkdirSync(path.join(targetDir, folder), { recursive: true });
|
|
37
|
-
console.log(chalk.green(
|
|
55
|
+
console.log(chalk.green(`📁 Folder created: ${folder}`));
|
|
38
56
|
});
|
|
39
57
|
|
|
40
58
|
const files = {
|
|
41
59
|
'server.js': `import { app } from "./app.js";
|
|
42
|
-
import mongoose from "mongoose";
|
|
43
60
|
import dotenv from "dotenv";
|
|
44
61
|
|
|
45
62
|
dotenv.config();
|
|
@@ -50,9 +67,7 @@ app.listen(PORT, () => {
|
|
|
50
67
|
console.log(\`⚡ Server is running on port \${PORT}\`);
|
|
51
68
|
});
|
|
52
69
|
|
|
53
|
-
|
|
54
|
-
.then(() => console.log("⚡ Database connected"))
|
|
55
|
-
.catch(err => console.error("❌ Database connection error:", err));
|
|
70
|
+
console.log("⚡ Connected to ${isPostgres ? 'PostgreSQL' : 'MongoDB'}");
|
|
56
71
|
`,
|
|
57
72
|
|
|
58
73
|
'app.js': `import express from "express";
|
|
@@ -78,14 +93,16 @@ export { app };
|
|
|
78
93
|
`,
|
|
79
94
|
|
|
80
95
|
'.env.example': `PORT=5000
|
|
81
|
-
|
|
96
|
+
${isPostgres ?
|
|
97
|
+
`DATABASE_URL="postgresql://username:password@localhost:5432/${isCurrentDir ? path.basename(process.cwd()) : projectName}?schema=public"` :
|
|
98
|
+
`DB_URL=mongodb://localhost:27017/${isCurrentDir ? path.basename(process.cwd()) : projectName}`}
|
|
82
99
|
JWT_SECRET=your_jwt_secret_key_here
|
|
83
100
|
`,
|
|
84
101
|
|
|
85
102
|
'package.json': `{
|
|
86
|
-
"name": "${projectName}",
|
|
103
|
+
"name": "${isCurrentDir ? path.basename(process.cwd()) : projectName}",
|
|
87
104
|
"version": "1.0.0",
|
|
88
|
-
"description": "Express + MongoDB
|
|
105
|
+
"description": "Express backend with ${isPostgres ? 'PostgreSQL + Prisma' : 'MongoDB + Mongoose'}",
|
|
89
106
|
"main": "server.js",
|
|
90
107
|
"type": "module",
|
|
91
108
|
"scripts": {
|
|
@@ -98,52 +115,191 @@ JWT_SECRET=your_jwt_secret_key_here
|
|
|
98
115
|
"dotenv": "^16.4.5",
|
|
99
116
|
"express": "^4.19.2",
|
|
100
117
|
"jsonwebtoken": "^9.0.2",
|
|
101
|
-
"mongoose": "^8.3.1",
|
|
102
118
|
"morgan": "^1.10.0"
|
|
119
|
+
${isPostgres ?
|
|
120
|
+
`,\n "@prisma/client": "^5.15.0",
|
|
121
|
+
"prisma": "^5.15.0"` :
|
|
122
|
+
`,\n "mongoose": "^8.3.1"`}
|
|
103
123
|
},
|
|
104
124
|
"devDependencies": {
|
|
105
125
|
"nodemon": "^3.1.0"
|
|
106
126
|
}
|
|
107
127
|
}`,
|
|
108
128
|
|
|
109
|
-
'README.md': `# ${projectName}
|
|
129
|
+
'README.md': `# ${isCurrentDir ? path.basename(process.cwd()) : projectName}
|
|
130
|
+
|
|
131
|
+
A fast and modern CLI tool to scaffold ready-to-use **Node.js Express** backend projects.
|
|
132
|
+
|
|
133
|
+
Generate a clean, professional Express server with support for **MongoDB** and **PostgreSQL** in seconds.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## ✨ Features
|
|
110
138
|
|
|
111
|
-
|
|
139
|
+
- ⚡ Extremely fast project generation
|
|
140
|
+
- 📁 Clean and scalable folder structure
|
|
141
|
+
- 🗄️ **MongoDB + Mongoose** (default)
|
|
142
|
+
- 🐘 **PostgreSQL + Prisma** support
|
|
143
|
+
- 🔐 JWT authentication middleware included
|
|
144
|
+
- 📦 Automatic \`npm install\`
|
|
145
|
+
- 🌐 Install in current directory with \`.\`
|
|
146
|
+
- ✨ Full ES Modules support (\`"type": "module"\`)
|
|
147
|
+
- 🚀 Ready for production
|
|
112
148
|
|
|
113
|
-
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## Quick Start
|
|
152
|
+
|
|
153
|
+
### Create a new project (MongoDB - Default)
|
|
114
154
|
|
|
115
155
|
\`\`\`bash
|
|
116
|
-
|
|
117
|
-
|
|
156
|
+
npx create-server-nodejs my-app
|
|
157
|
+
\`\`\`
|
|
158
|
+
|
|
159
|
+
### Create with PostgreSQL
|
|
160
|
+
|
|
161
|
+
\`\`\`bash
|
|
162
|
+
npx create-server-nodejs my-app --postgresql
|
|
163
|
+
\`\`\`
|
|
164
|
+
|
|
165
|
+
### Install in current directory
|
|
166
|
+
|
|
167
|
+
\`\`\`bash
|
|
168
|
+
npx create-server-nodejs .
|
|
169
|
+
\`\`\`
|
|
170
|
+
|
|
171
|
+
## Project Structure
|
|
172
|
+
|
|
173
|
+
\`\`\`bash
|
|
174
|
+
my-app/
|
|
175
|
+
├── Controllers/ # Controllers and business logic
|
|
176
|
+
├── Models/ # Database models
|
|
177
|
+
├── Routers/ # API routes
|
|
178
|
+
├── Middlewares/ # Authentication, validation, error handling
|
|
179
|
+
├── Utils/ # Helper functions
|
|
180
|
+
├── config/ # Configuration files
|
|
181
|
+
├── prisma/ # Prisma schema (only when using PostgreSQL)
|
|
182
|
+
├── server.js
|
|
183
|
+
├── app.js
|
|
184
|
+
├── .env.example
|
|
185
|
+
├── package.json
|
|
186
|
+
└── README.md
|
|
187
|
+
\`\`\`
|
|
188
|
+
|
|
189
|
+
## How to Run
|
|
190
|
+
|
|
191
|
+
\`\`\`bash
|
|
192
|
+
cd my-app
|
|
193
|
+
|
|
194
|
+
# Copy environment variables
|
|
118
195
|
cp .env.example .env
|
|
196
|
+
|
|
197
|
+
# For PostgreSQL only:
|
|
198
|
+
# npx prisma generate
|
|
199
|
+
# npx prisma db push
|
|
200
|
+
|
|
201
|
+
# Run development server
|
|
119
202
|
npm run dev
|
|
120
203
|
\`\`\`
|
|
204
|
+
|
|
205
|
+
## Available Options
|
|
206
|
+
|
|
207
|
+
| Command | Description |
|
|
208
|
+
|---|---|
|
|
209
|
+
| \`npx create-server-nodejs <name>\` | Create a new project |
|
|
210
|
+
| \`npx create-server-nodejs .\` | Scaffold in current directory |
|
|
211
|
+
| \`--mongodb\` | Use MongoDB + Mongoose (default) |
|
|
212
|
+
| \`--postgresql\` | Use PostgreSQL + Prisma |
|
|
213
|
+
|
|
214
|
+
## Tech Stack
|
|
215
|
+
|
|
216
|
+
- Express.js
|
|
217
|
+
- MongoDB + Mongoose or PostgreSQL + Prisma
|
|
218
|
+
- JWT Authentication
|
|
219
|
+
- bcryptjs, cors, morgan, dotenv
|
|
220
|
+
- Nodemon (dev dependency)
|
|
221
|
+
|
|
222
|
+
## License
|
|
223
|
+
This project is licensed under the MIT License.
|
|
224
|
+
|
|
225
|
+
Made with ❤️ for faster backend development
|
|
226
|
+
Happy coding!
|
|
121
227
|
`,
|
|
228
|
+
|
|
229
|
+
'Routers/user.routes.js': `import express from "express";
|
|
230
|
+
const router = express.Router();
|
|
231
|
+
|
|
232
|
+
router.get("/", (req, res) => {
|
|
233
|
+
res.json({ message: "User routes working!" });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
export default router;
|
|
237
|
+
`,
|
|
238
|
+
|
|
239
|
+
'Middlewares/auth.middleware.js': `export const protect = (req, res, next) => {
|
|
240
|
+
console.log("Auth middleware called");
|
|
241
|
+
next();
|
|
122
242
|
};
|
|
243
|
+
`,
|
|
123
244
|
|
|
245
|
+
'Utils/helpers.js': `export const generateRandomString = (length = 8) => {
|
|
246
|
+
return Math.random().toString(36).substring(2, length + 2);
|
|
247
|
+
};
|
|
248
|
+
`
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// Write all files
|
|
124
252
|
Object.keys(files).forEach(fileName => {
|
|
125
253
|
const filePath = path.join(targetDir, fileName);
|
|
126
254
|
fs.writeFileSync(filePath, files[fileName]);
|
|
127
|
-
console.log(chalk.green(
|
|
255
|
+
console.log(chalk.green(`📄 File created: ${fileName}`));
|
|
128
256
|
});
|
|
129
257
|
|
|
130
|
-
//
|
|
258
|
+
// Create Prisma schema for PostgreSQL
|
|
259
|
+
if (isPostgres) {
|
|
260
|
+
const prismaDir = path.join(targetDir, 'prisma');
|
|
261
|
+
fs.mkdirSync(prismaDir, { recursive: true });
|
|
262
|
+
|
|
263
|
+
fs.writeFileSync(path.join(prismaDir, 'schema.prisma'), `generator client {
|
|
264
|
+
provider = "prisma-client-js"
|
|
265
|
+
}
|
|
131
266
|
|
|
267
|
+
datasource db {
|
|
268
|
+
provider = "postgresql"
|
|
269
|
+
url = env("DATABASE_URL")
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
model User {
|
|
273
|
+
id Int @id @default(autoincrement())
|
|
274
|
+
email String @unique
|
|
275
|
+
password String
|
|
276
|
+
createdAt DateTime @default(now())
|
|
277
|
+
updatedAt DateTime @updatedAt
|
|
278
|
+
}
|
|
279
|
+
`);
|
|
280
|
+
console.log(chalk.green(`📄 File created: prisma/schema.prisma`));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Install dependencies automatically
|
|
132
284
|
console.log(chalk.yellow('\n📦 Installing dependencies... (This may take a while)'));
|
|
285
|
+
|
|
133
286
|
try {
|
|
134
|
-
execSync('npm install', {
|
|
135
|
-
cwd: targetDir,
|
|
136
|
-
stdio: 'inherit'
|
|
137
|
-
});
|
|
287
|
+
execSync('npm install', { cwd: targetDir, stdio: 'inherit' });
|
|
138
288
|
console.log(chalk.green('✅ Dependencies installed successfully!'));
|
|
289
|
+
|
|
290
|
+
if (isPostgres) {
|
|
291
|
+
console.log(chalk.yellow('\n🔧 Generating Prisma Client...'));
|
|
292
|
+
execSync('npx prisma generate', { cwd: targetDir, stdio: 'inherit' });
|
|
293
|
+
}
|
|
139
294
|
} catch (error) {
|
|
140
|
-
console.log(chalk.red('❌ Error during npm install
|
|
295
|
+
console.log(chalk.red('❌ Error during npm install'));
|
|
141
296
|
console.error(error.message);
|
|
297
|
+
process.exit(1);
|
|
142
298
|
}
|
|
143
299
|
|
|
144
|
-
|
|
300
|
+
console.log(chalk.bold.green('\n🎉 Project created successfully!'));
|
|
145
301
|
|
|
146
|
-
|
|
147
|
-
console.log(chalk.cyan(`\n cd ${projectName}`));
|
|
148
|
-
|
|
302
|
+
if (!isCurrentDir) {
|
|
303
|
+
console.log(chalk.cyan(`\n cd ${projectName}`));
|
|
304
|
+
}
|
|
149
305
|
console.log(chalk.cyan(` npm run dev\n`));
|