boilerexpressmvc v1.0.4
π π boilerexpressmvc Documentation - A Ready-to-Use Boilerplate for Express.js
Welcome to the
boilerexpressmvc
project! This is a comprehensive boilerplate designed to help you quickly build scalable MVC-based web applications and APIs using Express.js It comes pre-configured with essential packages, structures, and setup allowing you to hit the ground running with minimal configuration.Whether you're developing APIs or web applications, this boilerplate provides a robust starting point for your next project. Below is a help you get started with the boilerplate and make the most of its features.
β οΈ Instructions
Follow the instruction below to install the necessary packages and set up the MVC structure:
npm i boilerexpressmvc
node node_modules/boilerexpressmvc/setupBoilerPlate.js
β This will set up the required directories and files.
Getting Started
To get started with this template, follow the steps below:
1. Create the .env
File
Create an .env file in the root of your directory and fill in the required values. In this case, the api folder is your root directory, so create the .env file inside the api folder.
Sample .env
file data (fill in according to your requirements):
The .env file stores environment variables to keep sensitive information secure and manage configurations across different environments.
PORT = YOUR_SERVER_PORT
MONGODB_URI = YOUR_MONGODB_URI
JWT_SECRET = YOUR_JWT_SECRET
JWT_EXPIRES_IN = YOUR_JWT_EXPIRES_IN
API_KEY = YOUR_API_KEY
DB_USER = YOUR_DB_USER
DB_PASSWORD = YOUR_DB_PASSWORD
DB_NAME = YOUR_DB_NAME
DB_HOST = YOUR_DB_HOST
MAIL_HOST = YOUR_MAIL_HOST
MAIL_PORT = YOUR_MAIL_PORT
MAIL_USER = YOUR_MAIL_USER
MAIL_PASS = YOUR_MAIL_PASS
SESSION_SECRET = YOUR_SESSION_SECRET
CORS_ORIGIN = YOUR_CORS_ORIGIN
2. Install Dependencies
Before starting the server, make sure to install all the necessary dependencies.
npm install
3. Run Server
To start the server follow below command.
npm run dev
π Features:
- ποΈ Pre-configured MVC structure to easily start building APIs and web applications.
- π¦ Pre-created demoUser API that you can use and run immediately.
- π Essential development packages for security, validation, authentication, and database management.
- ποΈ MongoDB support for database interaction.
- π Environment variable management, CORS, and automatic server restarts during development.
π¦ Pre-installed Packages:
- The boilerplate comes with the following pre-installed packages that cover essential functionality like server monitoring, authentication, validation, and more:
Node.js Packages
This project utilizes various Node.js packages to enhance development and ensure security and functionality. Below is an overview of the key packages used in this project:
1. Nodemon π
- Nodemon is a development tool for Node.js that automatically restarts the server when changes are detected in the codebase. It eliminates the need to manually stop and restart the server, making the development process smoother and more efficient. It monitors files with specific extensions like
.js
,.json
,.mjs
,.coffee
, and more. This is particularly useful for testing APIs and making rapid changes without interrupting the server. Nodemon can be easily installed via npm and can be customized to suit project-specific needs.
2. Express β‘
- Express is a minimal and flexible Node.js web application framework designed to simplify the development of web applications and APIs. It provides a robust set of features like routing, middleware, and template engines, which reduce boilerplate code. With Express, developers can easily create RESTful APIs, handle HTTP requests, and manage server-side logic. It also supports integration with databases and front-end frameworks, allowing full-stack development. Express is widely used for building scalable and high-performance web applications.
3. BcryptJS π
- BcryptJS is a JavaScript library used for hashing passwords and ensuring secure storage of sensitive data. It implements the bcrypt hashing algorithm, which is known for its security features, including salt generation and adjustable hash complexity. With BcryptJS, developers can securely hash passwords before storing them in a database, as well as verify the hashed passwords during user authentication. This library helps protect user data and prevent common security vulnerabilities like rainbow table attacks.
4. CORS π
- CORS (Cross-Origin Resource Sharing) is a middleware that allows secure cross-origin requests in web applications. It enables resources from one domain to be accessed by web pages from another domain, which is necessary for APIs or web services to work across different origins. CORS helps control which domains can interact with your resources and provides fine-grained control over HTTP headers. Itβs a crucial tool for building modern web applications that interact with third-party services and APIs.
5. Dotenv π
- Dotenv is a package that helps manage environment variables by loading them from a
.env
file intoprocess.env
. This makes it easy to keep sensitive data like API keys, database credentials, and configuration settings out of the codebase. Using Dotenv, developers can easily switch between different environments (development, production, etc.) without hardcoding sensitive information. This package is simple to configure and integrates well with other Node.js packages, promoting better security and easier deployment.
6. Joi π
- Joi is a powerful data validation library for JavaScript. It allows developers to define and validate complex data structures using a clean, declarative syntax. With Joi, developers can easily validate user inputs, API request bodies, and database models to ensure the data is correct before processing. Joi provides many built-in validation methods, such as checking for required fields, string formats, numeric ranges, and custom rules. It helps reduce bugs and ensures that only valid data is passed through the system.
7. JSON Web Token (JWT) π‘οΈ
- JWT is a compact, URL-safe token format for securely transmitting information between a server and a client. It is widely used for authentication and authorization in modern web applications. JWTs contain claims (statements about an entity, like the user) and are signed using a secret or a public/private key pair. With JWT, users can authenticate once and then send the token in subsequent requests to access protected routes. JWTs are stateless and do not require session storage, making them ideal for scalable systems.
8. Mongoose π±
- Mongoose is an ODM (Object Data Modeling) library for MongoDB, designed to simplify data interaction. It provides a schema-based solution to model application data and enables seamless integration with MongoDB. Mongoose allows developers to define models, perform validations, and interact with databases using powerful query functions. It also supports middleware for actions like pre/post hooks for saving data, ensuring that business logic is executed before or after database operations. Mongoose is a popular choice for building applications with MongoDB.
9. fs-extra π
- fs-extra is a Node.js package that extends the functionality of the native
fs
module, adding more features for working with the file system. It provides methods for file operations like copying files or directories, removing files recursively, and checking if files or directories exist.fs-extra
simplifies tasks that require interacting with the file system, such as file manipulation and managing directories, and also improves reliability by handling edge cases. Itβs a valuable tool for developers working on file management features in Node.js applications.
π Environment Variables:
- PORT: Port your server will run on (e.g., 3000).
- MONGODB_URI: MongoDB connection string (e.g., mongodb://localhost:27017/mydatabase).
- JWT_SECRET: A strong, unique secret key used to sign JWT tokens.
- JWT_EXPIRES_IN: Token expiration time, e.g., 1h, 24h.
- API_KEY: Your third-party API key (e.g., for mail services).
- DB_USER, DB_PASSWORD, DB_NAME, DB_HOST: Credentials for connecting to your database.
- MAIL_HOST, MAIL_PORT, MAIL_USER, MAIL_PASS: Mail service configuration for sending emails.
- SESSION_SECRET: Secret key for managing user sessions.
- CORS_ORIGIN: Allowed origins for Cross-Origin Resource Sharing.
π Useful Links:
- π Express Documentation
- π MongoDB Documentation
- π οΈ JWT Documentation
- π Mongoose Documentation
- π Dotenv Documentation
- π Nodemon Documentation
- π BcryptJS Documentation
- β Joi Documentation
- π CORS Documentation
- π fs-extra Documentation
π Additional Notes:
This boilerplate template is designed to help you get up and running quickly with Express.js. Ensure that you update sensitive values in the .env file before running the app.
If you're using a production environment, make sure to follow proper security practices for handling API keys, secrets, and other sensitive data.
Explore the project:
- The server will automatically restart when changes are made. Check the demoUser API for example routes.