Basic Node.js Project Setup with MySQL and Sequelize
This package provides a complete Node.js project template with pre-configured MySQL, Sequelize, JWT authentication, email integration via Nodemailer, and Twilio OTP support. It simplifies project setup, allowing developers to focus on writing application logic.
Features
Express.js for routing and middleware.
Sequelize ORM pre-configured for MySQL database integration.
JWT for secure authentication.
Nodemailer for email services (e.g., SMTP, Gmail, OAuth2).
Twilio for OTP and SMS services.
A well-structured and scalable directory layout.
Example .env file for easy environment configuration.
Installation
- Install the Package
Run the following command to install the setup directly into your project:
npx mysql-code-generator <project-name>
cd <project-name>
- Install Dependencies
Ensure all dependencies are installed by navigating to the project directory and running:
npm install
npm start
- Configure the .env File
After installation, a .env file will be included in the root directory. Update the file with your specific environment variables.
Example .env file:
DATABASE_NAME = YOUR_DATABASE_NAME
DATABASE_USER = YOUR_DATABASE_USER
DATABASE_PASSWORD = YOUR_DATABASE_PASSWORD
DATABASE_HOST = YOUR_DATABASE_HOST
DIALECT = mysql
SECRET_KEY = your_random_secret_key_here
SALT = 10
SERVICE = YOUR_MAIL_SERVICE
MAIL_TYPE = YOUR_MAIL_TYPE
MAIL_USERNAME = YOUR_MAIL_USERNAME
MAIL_PASSWORD = YOUR_MAIL_PASSWORD
OAUTH_CLIENTID = YOUR_NODEMAILER_CLIENT_ID
OAUTH_CLIENT_SECRET = YOUR_NODEMAILER_CLIENT_SECRET
OAUTH_REFRESH_TOKEN = YOUR_REFRESH_TOKEN_FOR_NODEMAILER
TWILIO_ACCOUNT_SID = YOUR_TWILIO_ACCOUNT_SID
TWILIO_AUTH_TOKEN = YOUR_TWILIO_AUTH_TOKEN
TWILIO_SERVICE_SID = YOUR_TWILIO_SERVICE_SID
Directory Structure
├── config/
├── controllers/
├── helpers/
├── middlewares/
├── models/
├── public/
├── routes/
├── views/
├── app.js
├── dbConnection.js
├── package.json
├── package-lock.json
└── .env
Cluster Support in Latest Version
In the latest version, we have implemented clustering to handle increased traffic and maximize CPU usage efficiently.
Let’s Pretend You Own a Pizza Shop
You’re the only one working:
You take orders
You make the pizza
You serve the pizza
This is like a normal Node.js app — single-threaded, doing one thing at a time.
Now Let’s Say Business Is Booming…
Customers are coming in like crazy, and you can’t keep up!
Option 1: You Hire a Helper Just for One Job
Let’s say someone orders a birthday pizza that takes longer to make.
You say:
“Hey, I’ll hire someone to handle just this one order!”
This helper will:
Take the order
Make the pizza
Come back and say, “Done!”
This is like a Child Process in Node.js.
âś… Use it when:
You have a big job (like resizing images or running a slow task)
You don’t want your main app to stop
Option 2: You Hire Many Pizza Makers for All Orders
You now have 4 ovens (like 4 CPU cores) and you hire 4 cooks.
Each cook handles any order that comes in — so customers don’t have to wait long.
This is called Clustering in Node.js.
âś… Use it when:
Your app gets lots of users at once
You want to use all your computer power (CPU cores)
Quick Recap:
Real World Node.js Thing Why You Use It
One-time helper child_process Handle big or slow jobs
Multiple cooks cluster Handle lots of traffic
Why Use Clustering?
You have multiple CPU cores and want to utilize them fully.
You have many concurrent requests and want to handle them without blocking each other.
đź”’ Security Enhancements
âś… Express Rate Limit
Prevents abuse and DDoS attacks by limiting repeated requests from the same IP:
Limits each IP to 100 requests per 15 minutes by default.
Easy to configure.
âś… Helmet
Helps secure your app by setting various HTTP headers:
Includes protections like XSS, Clickjacking, Content Security Policy, and more.
Comes pre-configured with reasonable defaults, but also customizable.
The following npm packages are included in this setup:
bcrypt: For password hashing
cookie-parser: To parse cookies
crypto: Built-in Node.js cryptographic utilities
dotenv: For environment variable management
ejs: Embedded JavaScript templating
express: Fast, unopinionated web framework
express-fileupload: Middleware for file uploads
http-errors: For error handling
joi: Schema validation
jsonwebtoken: For JWT authentication
morgan: HTTP request logger
mysql2: MySQL database adapter
nodemailer: For sending emails
nodemon: Development server auto-reloader
node-twillo-otp-manager: Twilio for otp. https://npm.io/package/node-twillo-otp-manager
sequelize: ORM for MySQL
swagger-ui-express: For API documentation
Install any missing dependencies with:
npm install
Built With
Node.js
Express.js
Sequelize
MySQL
Nodemailer
Twilio
License
This project is licensed under the MIT License.