UNPKG

rest-setup

Version:

A ready-to-use REST API backend structure built with Node.js and Express โ€” includes organized folders for controllers, routes, models, and middlewares.

288 lines (216 loc) โ€ข 8.16 kB
# ๐Ÿš€ rest-setup <div align="center"> ![rest-setup Banner](https://ik.imagekit.io/vi5hal/REST-setup_%20API%20Made%20Easy.png?updatedAt=1761814367042) Create production-ready Node.js backends in seconds! A modern, feature-rich backend bootstrapper with best practices baked in. [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/vi5halsingh/rest-setup/blob/main/LICENSE) [![npm version](https://badge.fury.io/js/rest-setup.svg)](https://www.npmjs.com/package/rest-setup) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/vi5halsingh/rest-setup/pulls) [![Downloads](https://img.shields.io/npm/dt/rest-setup.svg)](https://www.npmjs.com/package/rest-setup) [Getting Started](#-getting-started) โ€ข [Features](#-features) โ€ข [Examples](#-examples) โ€ข [Documentation](#-documentation) โ€ข [Contributing](#-contributing) </div> --- ## ๐ŸŽฏ Why rest-setup? - ๐ŸŽจ **Clean Architecture**: Following industry-standard MVC patterns - ๐Ÿ”’ **Security First**: Built-in security best practices - ๐Ÿ“ฆ **Zero Config**: Start coding in seconds - ๐Ÿ› ๏ธ **Developer Experience**: Hot reload, error handling, and more - ๐Ÿ”Œ **Plug & Play**: Cloudinary, MongoDB, and more integrations - ๐Ÿ“ **Type Safety**: Optional TypeScript support ## ๏ฟฝ Getting Started ### One-Line Installation ```bash # Create a new project npx rest-setup my-api # Or initialize in current directory npx rest-setup . ``` ### What's Included? ``` ๐ŸŽฏ Initializing rest-setup... โšก๏ธ Creating project structure โ”œโ”€ Setting up MVC architecture โ”œโ”€ Configuring security middleware โ”œโ”€ Adding error handlers โ””โ”€ Installing dependencies ๐ŸŽจ Customizing your setup โ”œโ”€ Generating .env file โ”œโ”€ Setting up MongoDB connection โ”œโ”€ Configuring Cloudinary โ””โ”€ Adding TypeScript (optional) ๐Ÿš€ Launching development server โ””โ”€ Available at http://localhost:8000 Happy coding! ๐ŸŽ‰ ``` ### System Requirements - Node.js 14+ ([Download](https://nodejs.org/)) - MongoDB ([Download](https://www.mongodb.com/try/download/community) or [Atlas](https://www.mongodb.com/atlas/database)) - Git ([Download](https://git-scm.com/)) ## โšก๏ธ Quick Start Guide ### 1. Configure Environment ```bash cd my-api cp .env.example .env ``` Edit `.env` with your settings: ```env # ๏ฟฝ Essential Configuration PORT=8000 NODE_ENV=development # ๏ฟฝ Database Configuration MONGODB_URI=your_mongodb_uri # โ˜๏ธ Cloudinary Settings (Optional) CLOUDINARY_NAME=your_cloud_name CLOUDINARY_KEY=your_api_key CLOUDINARY_SECRET=your_api_secret ``` ### 2. Start Development ```bash # Install dependencies npm install # Start development server with hot reload npm run dev # Or production mode npm start ``` ## ๐ŸŽฏ Feature Highlights ### ๐Ÿ—๏ธ Core Features - **MVC Architecture**: Clean separation of concerns - **Express.js**: Fast, unopinionated web framework - **MongoDB Integration**: Ready-to-use database setup - **Error Handling**: Standardized error responses - **File Upload**: Built-in file handling with Multer - **Cloudinary**: Image upload and storage integration ### ๏ฟฝ Security & Middleware - **CORS**: Configurable cross-origin settings - **Cookie Parser**: HTTP cookie handling - **Request Parsing**: JSON and URL-encoded bodies - **Static Files**: Serve static content ### ๐Ÿ› ๏ธ Developer Experience - **Organized Structure**: Clear project layout - **API Response**: Standardized JSON responses - **Environment Config**: Easy .env configuration - **Prettier**: Code formatting included ## ๐Ÿ“˜ Documentation ## ๐Ÿ“ Project Architecture ```bash ๐Ÿ“ฆ rest-setup โ”œโ”€ ๐Ÿ“ src/ # Application source โ”‚ โ”œโ”€ ๐Ÿ“„ app.js # Express configuration โ”‚ โ”œโ”€ ๐Ÿ“„ index.js # Entry point โ”‚ โ”œโ”€ ๏ฟฝ constants.js # Application constants โ”‚ โ”œโ”€ ๐Ÿ“ controllers/ # Request handlers โ”‚ โ”‚ โ””โ”€ ๐Ÿ“„ user.controller.js โ”‚ โ”œโ”€ ๐Ÿ“ models/ # Data models โ”‚ โ”‚ โ””โ”€ ๐Ÿ“„ user.model.js โ”‚ โ”œโ”€ ๐Ÿ“ routes/ # API routes โ”‚ โ”‚ โ””โ”€ ๐Ÿ“„ user.routes.js โ”‚ โ”œโ”€ ๐Ÿ“ middlewares/ # Custom middleware โ”‚ โ”‚ โ””โ”€ ๐Ÿ“„ multer.middleware.js โ”‚ โ”œโ”€ ๐Ÿ“ db/ # Database configuration โ”‚ โ”‚ โ””โ”€ ๐Ÿ“„ db.js โ”‚ โ””โ”€ ๐Ÿ“ utils/ # Utility functions โ”‚ โ”œโ”€ ๐Ÿ“„ apiError.js # Error handling โ”‚ โ”œโ”€ ๐Ÿ“„ apiResponse.js # Response formatting โ”‚ โ””โ”€ ๐Ÿ“„ cloudinary.js # Cloudinary config โ”œโ”€ ๐Ÿ“ public/ # Static files โ”‚ โ””โ”€ ๐Ÿ“ temp/ # Temporary file storage โ”œโ”€ ๐Ÿ“„ .env.example # Environment template โ”œโ”€ ๐Ÿ“„ .gitignore # Git ignore rules โ”œโ”€ ๐Ÿ“„ .prettierrc # Prettier config โ””โ”€ ๐Ÿ“„ package.json # Dependencies ``` ### ๏ฟฝ File Upload Example ```javascript // Configure multer middleware const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, "./public/temp") }, filename: function (req, file, cb) { cb(null, file.originalname) } }) export const upload = multer({ storage, }) // Use in routes router.post("/upload", upload.single("file"), uploadToCloudinary ); ``` ### ๐ŸŽฏ Error Handling ```javascript class ApiError extends Error { constructor( statusCode, message= "Something went wrong", errors = [], stack = "" ){ super(message) this.statusCode = statusCode this.data = null this.message = message this.success = false; this.errors = errors if (stack) { this.stack = stack } else{ Error.captureStackTrace(this, this.constructor) } } } // Usage in controllers if (!user) { throw new ApiError(404, "User not found") } ``` ## ๐Ÿค Contributing We love your input! We want to make contributing as easy and transparent as possible. Check out our [Contributing Guide](CONTRIBUTING.md) for detailed guidelines. ### ๐ŸŽฏ Development Process 1. Fork the repo and create your branch from `main` 2. Install dependencies: `npm install` 3. Add your changes 4. Run tests: `npm test` 5. Submit a PR! ### ๐Ÿ“ Code Style We use ESLint and Prettier to maintain code quality. Before committing: ```bash # Format code npm run format # Run linter npm run lint # Run tests npm run test ``` ## ๐Ÿ“š Additional Resources - [API Documentation](docs/API.md) - [Security Guide](docs/SECURITY.md) - [Deployment Guide](docs/DEPLOYMENT.md) - [Change Log](CHANGELOG.md) ## ๐ŸŒŸ Stargazers [![Stargazers repo roster for @vi5halsingh/rest-setup](https://reporoster.com/stars/vi5halsingh/rest-setup)](https://github.com/vi5halsingh/rest-setup/stargazers) ## ๐Ÿ“„ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## ๏ฟฝ Support <a href="https://www.buymeacoffee.com/vi5halsingh" target="_blank"> <img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" width="150"> </a> ## ๐Ÿ“ซ Connect & Contribute <div align="center"> [![GitHub](https://img.shields.io/github/followers/vi5halsingh?label=Follow&style=social)](https://github.com/vi5halsingh) [![Twitter](https://img.shields.io/twitter/follow/vi5halsingh?style=social)](https://x.com/Vi5hu_) [![LinkedIn](https://img.shields.io/badge/LinkedIn-Connect-blue)](https://linkedin.com/in/vi5halsingh) [Report Bug](https://github.com/vi5halsingh/rest-setup/issues) ยท [Request Feature](https://github.com/vi5halsingh/rest-setup/issues) </div> --- <div align="center"> Made with code and coffee by <a href="https://github.com/vi5halsingh">Vishal Singh</a> If you found this project helpful, please consider giving it a โญ๏ธ </div>