backend-scaffold-cli 1.6.0 → 1.8.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 +42 -36
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -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',
@@ -156,6 +162,7 @@ const createConfigFiles = (projectPath, answers) => {
156
162
  mongoose: '^7.0.0',
157
163
  dotenv: '^16.0.3',
158
164
  cors: '^2.8.5',
165
+ helmet: '^7.1.0',
159
166
  ...(answers.useAuth && { jsonwebtoken: '^9.0.0' , bcryptjs: '^2.4.3'}),
160
167
  ...(answers.useValidation && { joi: '^17.9.0' }),
161
168
  ...(answers.useRateLimit && { 'express-rate-limit': '^6.7.0' }),
@@ -186,11 +193,7 @@ NODE_ENV=development
186
193
  # MongoDB Configuration
187
194
  MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/myappdb
188
195
  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
196
+ ${answers.useDNSFix ? '\n# DNS Configuration (for MongoDB connection issues)\nDNS_SERVERS=8.8.8.8,8.8.4.4' : ''}
194
197
 
195
198
  # JWT Configuration
196
199
  JWT_SECRET=your-secret-key-change-this-in-production
@@ -201,6 +204,7 @@ CORS_ORIGIN=http://localhost:3000
201
204
 
202
205
  # Logging
203
206
  LOG_LEVEL=debug
207
+ ${answers.useAxios ? '\n# External API\nAPI_BASE_URL=' : ''}
204
208
  `;
205
209
 
206
210
  fs.writeFileSync(path.join(projectPath, '.env.example'), env);
@@ -522,34 +526,23 @@ module.exports = router;
522
526
 
523
527
 
524
528
  const dbConfig = `const mongoose = require('mongoose');
525
- const dns = require('dns');
529
+ ${answers.useDNSFix ? "const dns = require('dns');" : ''}
526
530
 
527
- // Set DNS servers
531
+ ${answers.useDNSFix ? `// Set custom DNS servers (fixes connection issues on some networks)
528
532
  const dnsServers = (process.env.DNS_SERVERS || '8.8.8.8,8.8.4.4').split(',');
529
533
  dns.setServers(dnsServers);
534
+ ` : ''}
530
535
 
531
536
  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
- });
537
+ ${answers.useDNSFix ? "console.log(`Using DNS servers: ${dnsServers.join(', ')}`);" : ''}
538
+
539
+ const conn = await mongoose.connect(process.env.MONGODB_URI, {
540
+ serverSelectionTimeoutMS: 5000,
541
+ socketTimeoutMS: 45000,
542
+ });
541
543
 
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
- }
544
+ console.log(\`MongoDB Connected: \${conn.connection.host}\`);
545
+ return conn;
553
546
  };
554
547
 
555
548
  module.exports = connectDB;
@@ -566,6 +559,7 @@ module.exports = connectDB;
566
559
 
567
560
  const server = `require('dotenv').config();
568
561
  const express = require('express');
562
+ const helmet = require('helmet');
569
563
  const cors = require('cors');
570
564
  const connectDB = require('./config/db');
571
565
  const routes = require('./routes');
@@ -574,18 +568,19 @@ const { errorMiddleware } = require('./middleware/errorHandler');
574
568
 
575
569
  const app = express();
576
570
  ${answers.useRateLimit ? "const limiter = require('./middleware/rateLimiter');" : ''}
571
+
577
572
  const PORT = process.env.PORT || 5000;
578
573
 
579
- // Connect to MongoDB
580
- connectDB();
574
+
581
575
 
582
576
  // Middleware
577
+ app.use(helmet());
583
578
  app.use(cors({
584
579
  origin: process.env.CORS_ORIGIN || '*'
585
580
  }));
586
581
 
587
- app.use(express.json());
588
- app.use(express.urlencoded({ extended: true }));
582
+ app.use(express.json({limit: '10kb'}));
583
+ app.use(express.urlencoded({ extended: true, limit: '10kb' }));
589
584
  app.use(loggerMiddleware);
590
585
  ${answers.useRateLimit ? 'app.use(limiter);' : ''}
591
586
 
@@ -604,11 +599,22 @@ app.get('/', (req, res) => {
604
599
  app.use(errorMiddleware);
605
600
 
606
601
 
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
- });
602
+ //connect to MongoDB
603
+ const startServer = async() =>{
604
+ try{
605
+ await connectDB();
606
+ app.listen(PORT, () =>{
607
+ console.log(\` Server running on port \${PORT}\`);
608
+ console.log(\`Environment: \${process.env.NODE_ENV || 'development'}\`);
609
+ });
610
+ }catch(error){
611
+ console.error('Failed to start server:', error.message);
612
+ process.exit(1);
613
+ }
614
+ }
615
+
616
+
617
+ startServer();
612
618
 
613
619
  process.on('unhandledRejection', (err) => {
614
620
  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.8.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": {