create-server-nodejs 1.0.1 → 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 +188 -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
|
@@ -4,23 +4,42 @@ import fs from 'fs';
|
|
|
4
4
|
import path from 'path';
|
|
5
5
|
import { fileURLToPath } from 'url';
|
|
6
6
|
import chalk from 'chalk';
|
|
7
|
+
import { execSync } from 'child_process';
|
|
7
8
|
|
|
8
9
|
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
-
const __dirname = path.dirname(__filename);
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
// Get project name or current directory (.)
|
|
12
|
+
let projectName = process.argv[2];
|
|
12
13
|
|
|
13
14
|
if (!projectName) {
|
|
14
15
|
console.log(chalk.red('❌ Please enter project name:'));
|
|
15
|
-
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 .'));
|
|
16
19
|
process.exit(1);
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
const
|
|
22
|
+
const args = process.argv.slice(3);
|
|
23
|
+
const useMongo = args.includes('--mongodb') || !args.includes('--postgresql');
|
|
24
|
+
const usePostgres = args.includes('--postgresql');
|
|
20
25
|
|
|
21
|
-
|
|
26
|
+
const dbType = usePostgres ? 'postgresql' : 'mongodb';
|
|
27
|
+
const isPostgres = dbType === 'postgresql';
|
|
22
28
|
|
|
23
|
-
|
|
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
|
+
}
|
|
24
43
|
|
|
25
44
|
const folders = [
|
|
26
45
|
'Controllers',
|
|
@@ -33,12 +52,11 @@ const folders = [
|
|
|
33
52
|
|
|
34
53
|
folders.forEach(folder => {
|
|
35
54
|
fs.mkdirSync(path.join(targetDir, folder), { recursive: true });
|
|
36
|
-
console.log(chalk.green(
|
|
55
|
+
console.log(chalk.green(`📁 Folder created: ${folder}`));
|
|
37
56
|
});
|
|
38
57
|
|
|
39
58
|
const files = {
|
|
40
59
|
'server.js': `import { app } from "./app.js";
|
|
41
|
-
import mongoose from "mongoose";
|
|
42
60
|
import dotenv from "dotenv";
|
|
43
61
|
|
|
44
62
|
dotenv.config();
|
|
@@ -49,9 +67,7 @@ app.listen(PORT, () => {
|
|
|
49
67
|
console.log(\`⚡ Server is running on port \${PORT}\`);
|
|
50
68
|
});
|
|
51
69
|
|
|
52
|
-
|
|
53
|
-
.then(() => console.log("⚡ Database connected"))
|
|
54
|
-
.catch(err => console.error("❌ Database connection error:", err));
|
|
70
|
+
console.log("⚡ Connected to ${isPostgres ? 'PostgreSQL' : 'MongoDB'}");
|
|
55
71
|
`,
|
|
56
72
|
|
|
57
73
|
'app.js': `import express from "express";
|
|
@@ -77,14 +93,16 @@ export { app };
|
|
|
77
93
|
`,
|
|
78
94
|
|
|
79
95
|
'.env.example': `PORT=5000
|
|
80
|
-
|
|
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}`}
|
|
81
99
|
JWT_SECRET=your_jwt_secret_key_here
|
|
82
100
|
`,
|
|
83
101
|
|
|
84
102
|
'package.json': `{
|
|
85
|
-
"name": "${projectName}",
|
|
103
|
+
"name": "${isCurrentDir ? path.basename(process.cwd()) : projectName}",
|
|
86
104
|
"version": "1.0.0",
|
|
87
|
-
"description": "Express + MongoDB
|
|
105
|
+
"description": "Express backend with ${isPostgres ? 'PostgreSQL + Prisma' : 'MongoDB + Mongoose'}",
|
|
88
106
|
"main": "server.js",
|
|
89
107
|
"type": "module",
|
|
90
108
|
"scripts": {
|
|
@@ -97,52 +115,191 @@ JWT_SECRET=your_jwt_secret_key_here
|
|
|
97
115
|
"dotenv": "^16.4.5",
|
|
98
116
|
"express": "^4.19.2",
|
|
99
117
|
"jsonwebtoken": "^9.0.2",
|
|
100
|
-
"mongoose": "^8.3.1",
|
|
101
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"`}
|
|
102
123
|
},
|
|
103
124
|
"devDependencies": {
|
|
104
125
|
"nodemon": "^3.1.0"
|
|
105
126
|
}
|
|
106
127
|
}`,
|
|
107
128
|
|
|
108
|
-
'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
|
|
109
138
|
|
|
110
|
-
|
|
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
|
|
111
148
|
|
|
112
|
-
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## Quick Start
|
|
152
|
+
|
|
153
|
+
### Create a new project (MongoDB - Default)
|
|
113
154
|
|
|
114
155
|
\`\`\`bash
|
|
115
|
-
|
|
116
|
-
|
|
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
|
|
117
195
|
cp .env.example .env
|
|
196
|
+
|
|
197
|
+
# For PostgreSQL only:
|
|
198
|
+
# npx prisma generate
|
|
199
|
+
# npx prisma db push
|
|
200
|
+
|
|
201
|
+
# Run development server
|
|
118
202
|
npm run dev
|
|
119
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!
|
|
120
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();
|
|
121
242
|
};
|
|
243
|
+
`,
|
|
122
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
|
|
123
252
|
Object.keys(files).forEach(fileName => {
|
|
124
253
|
const filePath = path.join(targetDir, fileName);
|
|
125
254
|
fs.writeFileSync(filePath, files[fileName]);
|
|
126
|
-
console.log(chalk.green(
|
|
255
|
+
console.log(chalk.green(`📄 File created: ${fileName}`));
|
|
127
256
|
});
|
|
128
257
|
|
|
129
|
-
//
|
|
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
|
+
}
|
|
130
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
|
|
131
284
|
console.log(chalk.yellow('\n📦 Installing dependencies... (This may take a while)'));
|
|
285
|
+
|
|
132
286
|
try {
|
|
133
|
-
execSync('npm install', {
|
|
134
|
-
cwd: targetDir,
|
|
135
|
-
stdio: 'inherit'
|
|
136
|
-
});
|
|
287
|
+
execSync('npm install', { cwd: targetDir, stdio: 'inherit' });
|
|
137
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
|
+
}
|
|
138
294
|
} catch (error) {
|
|
139
|
-
console.log(chalk.red('❌ Error during npm install
|
|
295
|
+
console.log(chalk.red('❌ Error during npm install'));
|
|
140
296
|
console.error(error.message);
|
|
297
|
+
process.exit(1);
|
|
141
298
|
}
|
|
142
299
|
|
|
143
|
-
|
|
300
|
+
console.log(chalk.bold.green('\n🎉 Project created successfully!'));
|
|
144
301
|
|
|
145
|
-
|
|
146
|
-
console.log(chalk.cyan(`\n cd ${projectName}`));
|
|
147
|
-
|
|
302
|
+
if (!isCurrentDir) {
|
|
303
|
+
console.log(chalk.cyan(`\n cd ${projectName}`));
|
|
304
|
+
}
|
|
148
305
|
console.log(chalk.cyan(` npm run dev\n`));
|