A comprehensive collection of Express.js backend templates showcasing modern web development practices with both JavaScript and TypeScript implementations. This repository serves as a complete learning resource and production-ready foundation for building scalable backend applications.
This repository contains two complete Express.js backend implementations:
- π‘ JavaScript Version: Traditional JavaScript implementation with ES6+ features
- π· TypeScript Version: Modern TypeScript implementation with full type safety
Both projects demonstrate the same core functionality but showcase different development approaches, allowing you to choose the best fit for your project requirements.
- β User Authentication System (Register/Login)
- β JWT Token-Based Security
- β Password Hashing & Encryption
- β MongoDB Integration with Mongoose
- β RESTful API Design
- β CORS Configuration
- β Environment-Based Configuration
- β Middleware Architecture
- β Error Handling & Validation
-
8000
- π§ Hot Reloading (Development Mode)
- π Comprehensive Documentation
- π§ͺ Production-Ready Setup
- π Security Best Practices
- π Clean Architecture Pattern
- π― API Testing Examples
Backend-Express-Tutorial/
βββ π JavaScript/ # JavaScript Implementation
β βββ π server.js # Main server file
β βββ π package.json # Dependencies & scripts
β βββ π config/ # Configuration files
β βββ π controllers/ # Business logic
β βββ π middleware/ # Custom middleware
β βββ π models/ # Database models
β βββ π routes/ # API routes
β βββ π README.md # JavaScript-specific docs
β
βββ π TypeScript/ # TypeScript Implementation
β βββ π tsconfig.json # TypeScript configuration
β βββ π package.json # Dependencies & scripts
β βββ π src/ # Source code
β β βββ π index.ts # Application entry point
β β βββ π server.ts # Express server setup
β β βββ π config/ # Configuration files
β β βββ π controllers/ # Business logic with types
β β βββ π middleware/ # Type-safe middleware
β β βββ π models/ # Mongoose models with types
β β βββ π routes/ # Type-safe routes
β β βββ π types/ # Type definitions
β βββ π README.md # TypeScript-specific docs
β
βββ π LICENSE # MIT License
βββ π .gitignore # Git ignore rules
βββ π README.md # This file
git clone https://github.com/NhanPhamThanh-IT/Backend-Express-Tutorial.git
cd Backend-Express-Tutorial
cd JavaScript
npm install
# Create .env file with your configuration
npm run dev
cd TypeScript
npm install
# Create .env file with your configuration
npm run dev
Create a .env
file in your chosen project directory:
PORT=5000
MONGODB_URI=mongodb://localhost:27017/your-database
JWT_SECRET=your-super-secret-jwt-key
CLIENT_URL=http://localhost:3000
NODE_ENV=development
Your server will be running at http://localhost:5000
Both implementations provide the same API endpoints:
Method | Endpoint | Description | Request Body |
---|---|---|---|
POST |
/api/v1/auth/register |
Register new user | { fullName, email, password } |
POST |
/api/v1/auth/login |
User login | { email, password } |
Register a new user:
curl -X POST http://localhost:5000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"fullName": "John Doe",
"email": "john@example.com",
"password": "securePassword123"
}'
Login user:
curl -X POST http://localhost:5000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "john@example.com",
"password": "securePassword123"
}'
Feature | JavaScript | TypeScript |
---|---|---|
Type Safety | Runtime checking | Compile-time checking |
Development Speed | Faster initial setup | Slower initial setup |
Maintainability | Good | Excellent |
IDE Support | Good | Exceptional |
Learning Curve | Lower | Higher |
Production Benefits | Good | Better error prevention |
Team Development | Good | Better with large teams |
- Express.js: Fast, unopinionated web framework
- MongoDB: NoSQL database
- Mongoose: MongoDB object modeling
- JWT: JSON Web Token authentication
- bcryptjs: Password hashing library
- Nodemon: Auto-restart development server
- TypeScript: (TypeScript version) Static type checking
- ts-node: (TypeScript version) TypeScript execution
- CORS: Cross-Origin Resource Sharing
- body-parser: Request body parsing
- dotenv: Environment variable management
Both implementations follow clean architecture principles:
- Database connection setup
- Environment variable management
- Application configuration
- Business logic implementation
- Request/response handling
- Data validation and processing
- Authentication verification
- Request preprocessing
- Error handling
- Database schema definition
- Data relationships
- Model methods and validations
- API endpoint definitions
- Route-specific middleware
- Request routing logic
- Password Encryption: bcryptjs hashing
- JWT Authentication: Stateless token-based auth
- CORS Protection: Configurable cross-origin requests
- Input Validation: Request data validation
- Error Sanitization: Safe error responses
- Environment Isolation: Secure configuration management
- Start with the JavaScript version
- Understand Express.js fundamentals
- Learn MongoDB and Mongoose basics
- Grasp authentication concepts
- Explore the TypeScript version
- Learn type definitions and interfaces
- Understand middleware patterns
- Practice API testing and debugging
- Compare both implementations
- Implement additional features
- Add testing suites
- Deploy to production
cd JavaScript
npm install --production
npm start
cd TypeScript
npm install
npm run build
npm start
- Heroku: Easy deployment with git integration
- Vercel: Serverless deployment option
- DigitalOcean: VPS deployment
- AWS EC2: Scalable cloud deployment
- Railway: Simple cloud deployment
See the API documentation section above for cURL examples.
- Import the API endpoints
- Set up environment variables
- Test registration and login flows
- Verify JWT token authentication
Create a .http
file with:
### Register User
POST http://localhost:5000/api/v1/auth/register
Content-Type: application/json
{
"fullName": "John Doe",
"email": "john@example.com",
"password": "securePassword123"
}
### Login User
POST http://localhost:5000/api/v1/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "securePassword123"
}
- v1.0.0 - Initial release with JavaScript and TypeScript implementations
- Features: Complete authentication system, MongoDB integration, JWT security
We welcome contributions! Here's how you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Follow the existing code style
- Add appropriate comments and documentation
- Test your changes thoroughly
- Update README if necessary
- Ensure both JavaScript and TypeScript versions work
- π§ͺ Testing: Add unit and integration tests
- π Documentation: Improve or translate documentation
- π Security: Enhance security features
- β‘ Performance: Optimize code performance
- π¨ UI: Add frontend examples
- π Features: Implement new functionality
- π Documentation: Check the README files in each project
- π Issues: Open an issue
- π¬ Discussions: GitHub Discussions
- β Star this repository if you find it helpful
- π΄ Fork to create your own version
- π’ Share with other developers
This project is licensed under the MIT License - see the LICENSE file for details.
- β Commercial use
- β Modification
- β Distribution
- β Private use
- β Liability
- β Warranty
Nhan Pham Thanh
- π GitHub: @NhanPhamThanh-IT
- π§ Email: Contact via GitHub
- πΌ LinkedIn: Connect on LinkedIn
Special thanks to:
- Express.js Team for the amazing framework
- MongoDB Team for the robust database solution
- TypeScript Team for bringing type safety to JavaScript
- Open Source Community for continuous inspiration
- Contributors who help improve this project
- Express.js Documentation
- MongoDB Documentation
- Mongoose Documentation
- TypeScript Documentation
- JWT.io
β Star this repository if it helped you build better Express.js applications! β
Made with β€οΈ by Nhan Pham Thanh
Building the future, one API at a time π