mvc-nodejs-generator
v1.0.1
Published
A CLI tool to generate MVC folder structure with boilerplate code for Node.js applications
Maintainers
Readme
MVC Node.js Generator
A powerful command-line tool to generate a complete MVC (Model-View-Controller) structure for Node.js applications with essential boilerplate code, authentication, and best practices built-in.
Features
- 🏗️ Complete MVC architecture setup
- 🔐 Authentication system with JWT
- 🎨 Multiple view engine support (EJS, Pug, Handlebars)
- 🔒 Security features (Helmet, Rate Limiting, etc.)
- 📝 Input validation and sanitization
- 🗃️ MongoDB integration with Mongoose
- 🎯 RESTful API structure
- 📱 Responsive CSS boilerplate
- ⚡ Development ready with nodemon
- 🔍 Error handling middleware
- 📦 Modern ES6+ syntax
Prerequisites
- Node.js (v14 or higher)
- MongoDB (local or remote)
- npm or yarn
Installation
To install the package globally, run:
npm install -g mvc-nodejs-generatorUsage
You can create a new MVC project in two ways:
- With a project name:
create-mvc-app my-project- Without a project name (you'll be prompted):
create-mvc-appThe CLI will guide you through the setup process, asking for:
Project Configuration
- Project name (default: my-mvc-app)
- View engine preference (EJS, Pug, or Handlebars)
Database Configuration
You have three options for MongoDB setup:
Local MongoDB
- Host: localhost (default)
- Port: 27017 (default)
- Database name
- Username (optional)
- Password (optional)
MongoDB Atlas (Cloud)
- Host (e.g., cluster0.xxxxx.mongodb.net)
- Database name
- Username
- Password
Custom MongoDB URL
- Full MongoDB connection URI
- Example local:
mongodb://localhost:27017/mydb - Example Atlas:
mongodb+srv://username:[email protected]/mydb
MongoDB Connection Examples
- Local MongoDB without authentication
Host: localhost
Port: 27017
Database: my-app
Result URI: mongodb://localhost:27017/my-app- Local MongoDB with authentication
Host: localhost
Port: 27017
Database: my-app
Username: myuser
Password: mypassword
Result URI: mongodb://myuser:mypassword@localhost:27017/my-app- MongoDB Atlas
Host: cluster0.xxxxx.mongodb.net
Database: my-app
Username: atlasuser
Password: atlaspassword
Result URI: mongodb+srv://atlasuser:[email protected]/my-appProject Structure
my-project/
├── app.js # Application entry point
├── package.json # Project dependencies and scripts
├── .env # Environment variables
├── .gitignore # Git ignore rules
├── controllers/ # Route controllers
│ ├── homeController.js
│ └── userController.js
├── models/ # Database models
│ └── User.js
├── views/ # View templates
│ ├── layouts/
│ │ └── main.ejs
│ ├── partials/
│ │ ├── header.ejs
│ │ └── footer.ejs
│ └── index.ejs
├── routes/ # Route definitions
│ ├── index.js
│ └── users.js
├── config/ # Configuration files
│ └── database.js
├── middlewares/ # Custom middleware
│ ├── auth.js
│ └── validators.js
├── public/ # Static files
│ ├── css/
│ │ └── style.css
│ ├── js/
│ └── images/
├── utils/ # Utility functions
└── services/ # Business logic servicesFeatures in Detail
Authentication
- JWT-based authentication system
- User registration and login endpoints
- Protected routes middleware
- Password hashing with bcrypt
Security
- Helmet.js for security headers
- Rate limiting for API endpoints
- CORS configuration
- Input validation and sanitization
- Secure password handling
Database
- MongoDB connection with Mongoose
- User model with schema validation
- Connection error handling
- Graceful shutdown
API Structure
- RESTful endpoints
- Structured route handling
- Controller-based logic
- Error handling middleware
Frontend
- Responsive CSS boilerplate
- Modular view structure
- Partial views support
- Multiple view engine support
Getting Started
After creating your project:
- Navigate to your project directory:
cd my-project- Install dependencies:
npm install- Configure your environment:
The
.envfile is created automatically with your database configuration, but you should:
- Update the
JWT_SECRETwith a secure key - Modify other variables as needed:
PORT=3000 MONGODB_URI=your_generated_mongodb_uri JWT_SECRET=your_jwt_secret_key NODE_ENV=development
- Start the development server:
npm run devAvailable Scripts
npm start: Start the production servernpm run dev: Start the development server with nodemon
API Endpoints
Public Routes
POST /api/users/register: Register a new userPOST /api/users/login: Login userGET /: Home page
Protected Routes
GET /api/users/profile: Get user profile (requires authentication)
Customization
Adding New Models
- Create a new file in the
modelsdirectory - Define your Mongoose schema
- Add validation rules
- Export the model
Adding New Routes
- Create a route file in
routesdirectory - Define your endpoints
- Create corresponding controllers
- Add to
app.js
Middleware
- Add custom middleware in
middlewaresdirectory - Register in
app.jsor specific routes
Best Practices
Security
- Always validate user input
- Use environment variables for sensitive data
- Implement rate limiting for APIs
- Keep dependencies updated
Code Organization
- Follow MVC pattern
- Use async/await for asynchronous operations
- Implement error handling
- Use middleware for common operations
Database
- Use Mongoose middleware for hooks
- Implement proper indexing
- Handle connection errors
- Use appropriate MongoDB connection options
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
