backend-scaffold-cli 1.5.0 → 1.6.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 (3) hide show
  1. package/README.md +66 -24
  2. package/bin/cli.js +106 -1
  3. package/package.json +5 -2
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
+
1
2
  # backend-scaffold-cli
2
3
 
3
- A CLI tool to quickly scaffold Express.js backend projects with MongoDB, middleware, and common setup. It automatically generates production-ready projects with JWT authentication, error handling, request logging, input validation, DNS configuration, and pre-configured MongoDB Atlas connection. Perfect for developers who want to skip boilerplate and start coding immediately.
4
+ A CLI tool to quickly scaffold Express.js backend projects with MongoDB, middleware, and common setup. It automatically generates production-ready projects with JWT authentication, error handling, request logging, input validation, DNS configuration, and pre-configured MongoDB Atlas connection. Every project has different requirements, so this tool provides a solid, common foundation that developers can easily modify, extend, or trim according to their specific needs.
4
5
 
5
6
  ## Features
6
7
 
@@ -13,13 +14,17 @@ A CLI tool to quickly scaffold Express.js backend projects with MongoDB, middlew
13
14
 
14
15
  🔐 **Security**
15
16
  - JWT authentication middleware
17
+ - Password hashing with bcryptjs
16
18
  - Error handling middleware
17
19
  - CORS configuration
20
+ - Rate limiting (optional)
18
21
 
19
22
  📝 **Developer Experience**
20
23
  - Logging middleware
21
- - Input validation
22
- - Sample routes
24
+ - Input validation (optional)
25
+ - File upload support with multer (optional)
26
+ - Axios utility for external API calls (optional)
27
+ - Sample routes and User model
23
28
  - Git initialization
24
29
  - Auto dependency installation
25
30
 
@@ -38,55 +43,92 @@ npm install -g backend-scaffold-cli
38
43
  create-express-backend my-app
39
44
  ```
40
45
 
46
+ > **Tip:** Always use `@latest` to ensure you get the newest version with all features and bug fixes.
47
+
41
48
  ## Usage
42
49
 
43
50
  ```bash
44
- npx backend-scaffold-cli <project-name>
51
+ npx backend-scaffold-cli@latest <project-name>
45
52
  ```
46
53
 
47
54
  You'll be prompted to select features:
48
- - ✅ Include JWT authentication
49
- - ✅ Include logging middleware
55
+ - ✅ Include JWT authentication (adds bcryptjs + User model)
50
56
  - ✅ Include input validation
57
+ - ✅ Include rate limiting
58
+ - ✅ Include file upload support (multer)
59
+ - ✅ Include axios (for external API calls)
51
60
  - ✅ Install dependencies
52
61
 
62
+ After setup, copy `.env.example` to `.env`, configure your MongoDB URI, then run:
63
+
64
+ ```bash
65
+ npm start
66
+ ```
67
+
68
+ Test it's working:
69
+ ```bash
70
+ curl http://localhost:5000/api/health
71
+ ```
72
+
53
73
  ## Project Structure
74
+
54
75
  ```
55
76
  my-app/
56
77
  ├── src/
57
- ├── server.js
58
- ├── config/db.js
59
- ├── middleware/
60
- ├── routes/
61
- ├── controllers/
62
- ├── models/
63
- └── utils/
78
+ ├── config/
79
+ │ └── db.js
80
+ ├── middleware/
81
+ ├── errorHandler.js
82
+ ├── logger.js
83
+ ├── auth.js
84
+ │ ├── validation.js
85
+ │ │ ├── rateLimiter.js
86
+ │ │ └── upload.js
87
+ │ ├── routes/
88
+ │ │ └── index.js
89
+ │ ├── controllers/
90
+ │ ├── models/
91
+ │ │ └── User.js
92
+ │ ├── utils/
93
+ │ │ └── axiosUtil.js
94
+ │ ├── validation/
95
+ │ └── server.js
96
+ ├── uploads/
64
97
  ├── .env.example
65
98
  ├── .gitignore
66
99
  └── package.json
67
- ```
68
-
69
- ## Quick Start
70
100
 
71
- ```bash
72
- npx backend-scaffold-cli my-app
73
- cd my-app
74
- cp .env.example .env
75
- npm start
76
101
  ```
77
102
 
78
- Visit: `http://localhost:5000/api/health`
79
103
 
80
104
  ## Environment Variables
81
105
 
82
106
  ```env
107
+ # Server Configuration
83
108
  PORT=5000
84
109
  NODE_ENV=development
110
+
111
+ # MongoDB Configuration
85
112
  MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/dbname
86
- JWT_SECRET=your-secret-key
113
+ DB_NAME=myapp
114
+
115
+ # DNS Configuration
87
116
  DNS_SERVERS=8.8.8.8,8.8.4.4
117
+
118
+ # JWT Configuration
119
+ JWT_SECRET=your-secret-key-change-this-in-production
120
+ JWT_EXPIRE=7d
121
+
122
+ # CORS
123
+ CORS_ORIGIN=http://localhost:3000
88
124
  ```
89
125
 
126
+
127
+
90
128
  ## License
91
129
 
92
- MIT
130
+ MIT
131
+
132
+ ## Repository
133
+
134
+ https://github.com/abhi-0605/express-backend
package/bin/cli.js CHANGED
@@ -7,8 +7,9 @@ const inquirer = require('inquirer');
7
7
  const fs = require('fs-extra');
8
8
  const path = require('path');
9
9
  const { execSync } = require('child_process');
10
+ const packageJson = require('../package.json');
10
11
 
11
- const version = '1.0.0';
12
+ const version = packageJson.version;
12
13
 
13
14
  program
14
15
  .version(version)
@@ -82,6 +83,7 @@ program
82
83
  }
83
84
  if(answers.useAxios){
84
85
  createAxiosUtil(projectPath);
86
+ createAuthController(projectPath);
85
87
  }
86
88
 
87
89
  console.log(chalk.cyan(' Initializing git repository...'));
@@ -464,6 +466,7 @@ const createRouteFiles = (projectPath, answers) => {
464
466
  const routes = `const express = require('express');
465
467
  const router = express.Router();
466
468
  ${answers.useAuth ? "const authenticateToken = require('../middleware/auth');" : ''}
469
+ ${answers.useAuth ? "const {register,login} = require('../controllers/authController');" : ''}
467
470
  ${answers.useFileUpload ? "const upload = require('../middleware/upload');" : ''}
468
471
 
469
472
  // Public routes
@@ -471,6 +474,13 @@ router.get('/health', (req, res) => {
471
474
  res.json({ status: 'API is running' });
472
475
  });
473
476
 
477
+ ${answers.useAuth? `
478
+ // Auth routes
479
+ // Register and Login routes
480
+ router.post('/auth/register', register);
481
+ router.post('/auth/login', login);
482
+ ` : ''}
483
+
474
484
  ${answers.useAuth ? `
475
485
  // Protected routes
476
486
  router.get('/protected', authenticateToken, (req, res) => {
@@ -679,6 +689,101 @@ function createUserModel(projectPath) {
679
689
 
680
690
 
681
691
 
692
+ function createAuthController(projectPath) {
693
+ const authController = `const jwt= require('jsonwebtoken');
694
+ const User=require('../models/User');
695
+ const { ErrorHandler } = require('../middleware/errorHandler');
696
+
697
+ //generate JWT token
698
+ const generateToken = (userId) => {
699
+ return jwt.sign({ id: userId }, process.env.JWT_SECRET, {
700
+ expiresIn: process.env.JWT_EXPIRE || '7d'
701
+ });
702
+ };
703
+
704
+
705
+ //register user
706
+ const register= async(req,res,next) =>{
707
+ try{
708
+ const {name,email,password}=req.body;
709
+
710
+ const existingUser= await User.findOne({email});
711
+ if(existingUser){
712
+ return next(new ErrorHandler('Email already Registered',400));
713
+ }
714
+
715
+ const user= await User.create({name,email,password});
716
+ const tocken= generateToken(user._id);
717
+
718
+ res.status(201).json({
719
+ success:true,
720
+ message:'User registered successfully',
721
+ token,
722
+ user:{
723
+ id:user._id,
724
+ name:user.name,
725
+ email:user.email
726
+ }
727
+ });
728
+ }catch(error){
729
+ next(error);
730
+ }
731
+ };
732
+
733
+
734
+
735
+ //login user
736
+ const login =async(req,res,next) =>{
737
+ try{
738
+ const {email,password}=req.body;
739
+
740
+ const user=await User.findOne({email}).select('+password');
741
+ if(!user){
742
+ return next(new ErrorHandler('Invalid email or password',401));
743
+ }
744
+
745
+ const isMatch= await user.comparePassword(password);
746
+ if(!isMatch){
747
+ return next(new ErrorHandler('Invalid email or password',401));
748
+ }
749
+
750
+ const token=generateToken(user._id);
751
+
752
+ res.status(200).json({
753
+ success:true,
754
+ message:'User logged in successfully',
755
+ token,
756
+ user:{
757
+ id:user._id,
758
+ name:user.name,
759
+ email: user.email
760
+ }
761
+ });
762
+ }catch(error){
763
+ next(error);
764
+ }
765
+ }
766
+
767
+
768
+ module.exports = {
769
+ register,
770
+ login
771
+ };
772
+
773
+ `;
774
+
775
+
776
+
777
+ fs.writeFileSync(
778
+ path.join(projectPath, 'src/controllers/authController.js'),
779
+ authController
780
+ );
781
+
782
+ console.log(chalk.green('Auth controller created'));
783
+ }
784
+
785
+
786
+
682
787
  function createAxiosUtil(projectPath){
683
788
  const axiosUtil = ` const axios = require('axios');
684
789
 
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "backend-scaffold-cli",
3
- "version": "1.5.0",
3
+ "version": "1.6.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": {
7
7
  "create-express-backend": "./bin/cli.js"
8
8
  },
9
9
  "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1"
10
+ "test": "jest"
11
11
  },
12
12
  "keywords": [
13
13
  "express",
@@ -32,5 +32,8 @@
32
32
  "commander": "^11.0.0",
33
33
  "fs-extra": "^11.1.0",
34
34
  "inquirer": "^8.2.5"
35
+ },
36
+ "devDependencies": {
37
+ "jest": "^30.4.2"
35
38
  }
36
39
  }