1.0.1 ⢠Published 6 months ago
mvc-nodejs-generator v1.0.1
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-generator
Usage
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-app
The 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:password@cluster0.xxxxx.mongodb.net/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:atlaspassword@cluster0.xxxxx.mongodb.net/my-app
Project 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 services
Features 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
.env
file is created automatically with your database configuration, but you should:
- Update the
JWT_SECRET
with 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 dev
Available 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
models
directory - Define your Mongoose schema
- Add validation rules
- Export the model
Adding New Routes
- Create a route file in
routes
directory - Define your endpoints
- Create corresponding controllers
- Add to
app.js
Middleware
- Add custom middleware in
middlewares
directory - Register in
app.js
or 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