From 5c21226fbf5ba5ee7ef3f9d9f7774475b564fda0 Mon Sep 17 00:00:00 2001 From: abdinasir-Tman Date: Mon, 13 May 2024 19:06:33 +0100 Subject: [PATCH] feat: changed JS-PostgreSQL-TypeORM-NoAuth-Template to the tempalte --- .env | 17 +++++-- .gitignore | 48 ++++++++++++++++++- package.json | 17 +++---- src/app.js | 46 ++++++++++++++++-- src/config/{db.config.js => dbConfig.js} | 4 +- .../{initial.config.js => initialConfig.js} | 0 src/controllers/controller.js | 7 --- src/controllers/helloWorldController.js | 9 ++++ src/routes/helloWorldRoutes.js | 8 ++++ src/routes/router.js | 5 -- 10 files changed, 131 insertions(+), 30 deletions(-) rename src/config/{db.config.js => dbConfig.js} (87%) rename src/config/{initial.config.js => initialConfig.js} (100%) delete mode 100644 src/controllers/controller.js create mode 100644 src/controllers/helloWorldController.js create mode 100644 src/routes/helloWorldRoutes.js delete mode 100644 src/routes/router.js diff --git a/.env b/.env index 9061bc99..424129fe 100644 --- a/.env +++ b/.env @@ -3,17 +3,28 @@ # 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= # Database Configuration # ---------------------- # Provide the URL for connecting to the database. This should include the protocol, username, password, and host as applicable. - 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= \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5a30af86..d1bc7cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,49 @@ -.vscode +# Logs and Runtime data +logs/ +*.log + +# Dependency directories node_modules/ + +# 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 diff --git a/package.json b/package.json index 225925f8..848bd4e0 100644 --- a/package.json +++ b/package.json @@ -9,26 +9,27 @@ "dev": "nodemon ./src/app.js" }, "dependencies": { - "express": "^4.18.2", + "express": "^4.19.2", "express-async-handler": "^1.2.0", "express-rate-limit": "^7.2.0", "cookie-parser": "^1.4.6", "cors": "^2.8.5", "jsonwebtoken": "^9.0.2", - "dotenv": "^16.3.1", + "dotenv": "^16.4.5", "bcrypt": "^5.1.1", - "joi": "^17.10.0", + "joi": "^17.13.1", "chalk": "5.3.0", + "compression": "^1.7.4", "morgan": "^1.10.0", - "helmet": "^7.0.0", - "nodemailer": "^6.9.9", + "helmet": "^7.1.0", + "nodemailer": "^6.9.13", "uuidv4": "^6.2.13", - "pg": "^8.11.3", + "pg": "^8.11.5", "reflect-metadata": "^0.2.2", "typeorm": "0.3.20" }, "devDependencies": { - "yonode": "^1.0.0", - "nodemon": "^3.0.1" + "yonode": "^1.2.3", + "nodemon": "^3.1.0" } } diff --git a/src/app.js b/src/app.js index b93c5b2f..e99b6a97 100644 --- a/src/app.js +++ b/src/app.js @@ -1,16 +1,54 @@ // import the packages 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 "./config/db.config.js"; +import { port } from "./config/initialConfig.js"; +import "./config/dbConfig.js"; -// initializing the app +// Initializing the app const app = express(); +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 + +// 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()); -// rest of your code here +// 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: {}, + }); +}); app.listen(port, () => { diff --git a/src/config/db.config.js b/src/config/dbConfig.js similarity index 87% rename from src/config/db.config.js rename to src/config/dbConfig.js index 0fe7e11a..e83ecc56 100644 --- a/src/config/db.config.js +++ b/src/config/dbConfig.js @@ -1,6 +1,6 @@ import "reflect-metadata"; import { DataSource } from "typeorm"; -import { dbName, dbUrl } from "./initial.config.js"; +import { dbName, dbUrl } from "./initialConfig.js"; import chalk from "chalk"; const AppDataSource = new DataSource({ @@ -15,7 +15,7 @@ AppDataSource.initialize() console.log(`${chalk.green.bold('Connected')} to the database`); }) .catch((error) => - console.log(`${chalk.red.bold('Error')} connecting to database`, error); + console.log(`${chalk.red.bold('Error')} connecting to database`, error) ); export default AppDataSource; diff --git a/src/config/initial.config.js b/src/config/initialConfig.js similarity index 100% rename from src/config/initial.config.js rename to src/config/initialConfig.js diff --git a/src/controllers/controller.js b/src/controllers/controller.js deleted file mode 100644 index e641c1a7..00000000 --- a/src/controllers/controller.js +++ /dev/null @@ -1,7 +0,0 @@ -export const controller = async (req, res) => { - try { - - } catch (error) { - - } -}; \ No newline at end of file diff --git a/src/controllers/helloWorldController.js b/src/controllers/helloWorldController.js new file mode 100644 index 00000000..1e10d3e5 --- /dev/null +++ b/src/controllers/helloWorldController.js @@ -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", + }); + } +}; diff --git a/src/routes/helloWorldRoutes.js b/src/routes/helloWorldRoutes.js new file mode 100644 index 00000000..ff85ddba --- /dev/null +++ b/src/routes/helloWorldRoutes.js @@ -0,0 +1,8 @@ +import express from "express"; +import { helloWorld } from "../controllers/helloWorldController.js"; + +const helloWorldRouter = express.Router(); + +helloWorldRouter.get("/", helloWorld); + +export default helloWorldRouter; diff --git a/src/routes/router.js b/src/routes/router.js deleted file mode 100644 index 18b49770..00000000 --- a/src/routes/router.js +++ /dev/null @@ -1,5 +0,0 @@ -import express from "express"; - -const routerName = express.Router(); - -export default routerName; \ No newline at end of file