backend-scaffold-cli 1.6.0 → 1.7.0

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.
Files changed (2) hide show
  1. package/bin/cli.js +39 -37
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
 
4
4
  const { program } = require('commander');
@@ -32,6 +32,12 @@ program
32
32
  message: 'Include input validation?',
33
33
  default: true
34
34
  },
35
+ {
36
+ type: 'confirm',
37
+ name: 'useDNSFix',
38
+ message: 'Use custom DNS (fixes MongoDB connection issues on some networks)?',
39
+ default: true
40
+ },
35
41
  {
36
42
  type: 'confirm',
37
43
  name: 'useRateLimit',
@@ -186,11 +192,7 @@ NODE_ENV=development
186
192
  # MongoDB Configuration
187
193
  MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/myappdb
188
194
  DB_NAME=myapp
189
-
190
- # DNS Configuration
191
- DNS_SERVERS=8.8.8.8,8.8.4.4
192
- PRIMARY_DNS=8.8.8.8
193
- SECONDARY_DNS=8.8.4.4
195
+ ${answers.useDNSFix ? '\n# DNS Configuration (for MongoDB connection issues)\nDNS_SERVERS=8.8.8.8,8.8.4.4' : ''}
194
196
 
195
197
  # JWT Configuration
196
198
  JWT_SECRET=your-secret-key-change-this-in-production
@@ -201,6 +203,7 @@ CORS_ORIGIN=http://localhost:3000
201
203
 
202
204
  # Logging
203
205
  LOG_LEVEL=debug
206
+ ${answers.useAxios ? '\n# External API\nAPI_BASE_URL=' : ''}
204
207
  `;
205
208
 
206
209
  fs.writeFileSync(path.join(projectPath, '.env.example'), env);
@@ -522,34 +525,23 @@ module.exports = router;
522
525
 
523
526
 
524
527
  const dbConfig = `const mongoose = require('mongoose');
525
- const dns = require('dns');
528
+ ${answers.useDNSFix ? "const dns = require('dns');" : ''}
526
529
 
527
- // Set DNS servers
530
+ ${answers.useDNSFix ? `// Set custom DNS servers (fixes connection issues on some networks)
528
531
  const dnsServers = (process.env.DNS_SERVERS || '8.8.8.8,8.8.4.4').split(',');
529
532
  dns.setServers(dnsServers);
533
+ ` : ''}
530
534
 
531
535
  const connectDB = async () => {
532
- try {
533
- console.log(\`📡 Using DNS servers: \${dnsServers.join(', ')}\`);
534
-
535
- const conn = await mongoose.connect(process.env.MONGODB_URI, {
536
- useNewUrlParser: true,
537
- useUnifiedTopology: true,
538
- serverSelectionTimeoutMS: 5000,
539
- socketTimeoutMS: 45000,
540
- });
536
+ ${answers.useDNSFix ? "console.log(`Using DNS servers: ${dnsServers.join(', ')}`);" : ''}
537
+
538
+ const conn = await mongoose.connect(process.env.MONGODB_URI, {
539
+ serverSelectionTimeoutMS: 5000,
540
+ socketTimeoutMS: 45000,
541
+ });
541
542
 
542
- console.log(\`MongoDB Connected: \${conn.connection.host}\`);
543
- return conn;
544
- } catch (error) {
545
- console.error(' MongoDB Connection Error:', error.message);
546
-
547
- if (process.env.NODE_ENV === 'production') {
548
- process.exit(1);
549
- } else {
550
- console.warn('⚠ Warning: Running without MongoDB connection');
551
- }
552
- }
543
+ console.log(\`MongoDB Connected: \${conn.connection.host}\`);
544
+ return conn;
553
545
  };
554
546
 
555
547
  module.exports = connectDB;
@@ -576,16 +568,15 @@ const app = express();
576
568
  ${answers.useRateLimit ? "const limiter = require('./middleware/rateLimiter');" : ''}
577
569
  const PORT = process.env.PORT || 5000;
578
570
 
579
- // Connect to MongoDB
580
- connectDB();
571
+
581
572
 
582
573
  // Middleware
583
574
  app.use(cors({
584
575
  origin: process.env.CORS_ORIGIN || '*'
585
576
  }));
586
577
 
587
- app.use(express.json());
588
- app.use(express.urlencoded({ extended: true }));
578
+ app.use(express.json({limit: '10kb'}));
579
+ app.use(express.urlencoded({ extended: true, limit: '10kb' }));
589
580
  app.use(loggerMiddleware);
590
581
  ${answers.useRateLimit ? 'app.use(limiter);' : ''}
591
582
 
@@ -604,11 +595,22 @@ app.get('/', (req, res) => {
604
595
  app.use(errorMiddleware);
605
596
 
606
597
 
607
- // Start Server
608
- app.listen(PORT, () => {
609
- console.log(\` Server running on port \${PORT}\`);
610
- console.log(\`Environment: \${process.env.NODE_ENV || 'development'}\`);
611
- });
598
+ //connect to MongoDB
599
+ const startServer = async() =>{
600
+ try{
601
+ await connectDB();
602
+ app.listen(PORT, () =>{
603
+ console.log(\` Server running on port \${PORT}\`);
604
+ console.log(\`Environment: \${process.env.NODE_ENV || 'development'}\`);
605
+ });
606
+ }catch(error){
607
+ console.error('Failed to start server:', error.message);
608
+ process.exit(1);
609
+ }
610
+ }
611
+
612
+
613
+ startServer();
612
614
 
613
615
  process.on('unhandledRejection', (err) => {
614
616
  console.error('Unhandled Rejection:', err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-scaffold-cli",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "CLI tool to scaffold Express.js backend projects with MongoDB, middleware, and common setup",
5
5
  "main": "bin/cli.js",
6
6
  "bin": {