Skip to content

Commit

Permalink
feat: changed JS-MySQL-Sequelize-NoAuth-Template to the template
Browse files Browse the repository at this point in the history
  • Loading branch information
abdinasir-Tman committed May 13, 2024
1 parent 2aecc3d commit 18be58a
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 42 deletions.
22 changes: 16 additions & 6 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
# Specify the port number on which the server will listen for incoming connections.
# If not specified, the default port 8000 will be used.
# Default: 8000

SERVER_PORT=
SERVER_PORT=8000

# Database Configuration
# ----------------------
# Provide the URL for connecting to the database. This should include the protocol, username, password, and host as applicable.

DATABASE_URL=

# mysql://root:@localhost:3306/
DATABASE_URL=
# Specify the name of the database to which the application will connect.
# If not specified, the default database 'yonodeDB' will be used.
# Default: yonodeDB

DATABASE_NAME=
# Authentication and Security Configuration
# -----------------------------------------
# Define a secret key for JWT (JSON Web Token) authentication. This key is used to sign and verify JWTs for secure data transfer and access control.
# The JWT_SECRET_KEY should be a long, random string that is kept secure to ensure token integrity and prevent unauthorized access.
JWT_SECRET_KEY=

# Application Environment
# -----------------------
# Set the environment in which the Node.js application is running. Common values are 'development', 'production', and 'test'.
# This setting can influence the application's behavior, enabling or disabling certain features based on the environment.
# For example, in 'development' mode, more verbose logging might be enabled, whereas 'production' might focus on performance optimizations and error handling.
# Default: development (if not specified)
NODE_ENVIRONMENT=
51 changes: 49 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
.vscode
/node_modules
# Logs and Runtime data
logs/
*.log

# Dependency directories
node_modules/
package-lock

# Yarn and npm cache directories
.yarn/cache/
.npm/

# TypeScript cache
*.tsbuildinfo

# Optional cache directories
.eslintcache
.stylelintcache

# Environment variables
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# Build output
dist/
build/
out/

# Coverage directory used by tools like istanbul
coverage/

# Editor-specific files
.vscode/
.idea/
*.swp

# Temporary files
*.tmp
*.temp

# OS-specific files
.DS_Store
Thumbs.db

# Miscellaneous
.node_repl_history
.vscode-test
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@
"dev": "nodemon ./src/app.js"
},
"dependencies": {
"express": "^4.18.2",
"express-async-handler": "^1.2.0",
"express-rate-limit": "^7.2.0",
"bcrypt": "^5.1.1",
"chalk": "5.3.0",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-async-handler": "^1.2.0",
"express-rate-limit": "^7.2.0",
"helmet": "^7.1.0",
"joi": "^17.13.1",
"jsonwebtoken": "^9.0.2",
"dotenv": "^16.3.1",
"bcrypt": "^5.1.1",
"joi": "^17.10.0",
"chalk": "5.3.0",
"morgan": "^1.10.0",
"helmet": "^7.0.0",
"nodemailer": "^6.9.9",
"uuidv4": "^6.2.13",
"mysql2": "^3.9.1",
"sequelize": "^6.36.0"
"mysql2": "^3.9.7",
"nodemailer": "^6.9.13",
"sequelize": "^6.37.3",
"uuidv4": "^6.2.13"
},
"devDependencies": {
"yonode": "^1.0.0",
"nodemon": "^3.0.1"
"nodemon": "^3.1.0",
"yonode": "^1.2.3"
}
}
49 changes: 43 additions & 6 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,56 @@
// import the packages
import express from 'express';
import chalk from 'chalk';
import express from "express";
import chalk from "chalk";
import helmet from "helmet";
import cors from "cors";
import morgan from "morgan";
import rateLimit from "express-rate-limit";
import compression from "compression";
import cookieParser from "cookie-parser";

// import your files
import { port } from './config/initial.config.js';
import { connectDB } from './config/db.config.js';
import { port } from './config/initialConfig.js';
import { connectDB } from './config/dbConfig.js';
import './models/models.js';
import helloWorldRouter from './routes/helloWorldRouter.js';

// Initializing the app
const app = express();
app.use(express.json());
app.use(cookieParser());
// Essential security headers with Helmet
app.use(helmet());

// Enable CORS with default settings
app.use(cors());

// Logger middleware for development environment
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}

app.use(compression()); // Compress all routes

// rest of your code here
// Rate limiting to prevent brute-force attacks
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});
app.use(limiter);

// Built-in middleware for parsing JSON
app.use(express.json());

// Use your routes here
app.use("/api/helloworld", helloWorldRouter);

// Global error handler
app.use((err, req, res, next) => {
console.error(chalk.red(err.stack));
res.status(err.status || 500).json({
message: err.message || "Internal Server Error",
error: {},
});
});
// database connection
connectDB()

Expand Down
4 changes: 2 additions & 2 deletions src/config/db.config.js → src/config/dbConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from "chalk";
import { Sequelize } from "sequelize"
import { dbUrl } from "./initial.config.js";
import { dbUrl } from "./initialConfig.js";

export const sequelize = new Sequelize(dbUrl);
export const connectDB = async () => {
Expand All @@ -15,4 +15,4 @@ export const connectDB = async () => {
}
}

export default sequelize;
export default sequelize;
File renamed without changes.
7 changes: 0 additions & 7 deletions src/controllers/controller.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/controllers/helloWorldController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const helloWorld = async (req, res) => {
try {
res.send("Hello World!");
} catch (error) {
res.status(500).json({
message: "Internal Server Error",
});
}
};
8 changes: 8 additions & 0 deletions src/routes/helloWorldRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from "express";
import { helloWorld } from "../controllers/helloWorldController.js";

const helloWorldRouter = express.Router();

helloWorldRouter.get("/", helloWorld);

export default helloWorldRouter;
5 changes: 0 additions & 5 deletions src/routes/router.js

This file was deleted.

0 comments on commit 18be58a

Please sign in to comment.