From 88596684771e2204ba1ba650339b17f1e436c189 Mon Sep 17 00:00:00 2001 From: abdinasir-Tman Date: Mon, 13 May 2024 17:28:59 +0100 Subject: [PATCH] feat: updated JS-MongoDB-Prisma-NoAuth-Template to the template --- .env | 16 +++++- .gitignore | 49 ++++++++++++++++++- package.json | 19 +++---- src/app.js | 48 ++++++++++++++++-- .../{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 -- 9 files changed, 132 insertions(+), 29 deletions(-) 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 b8ee1ec6..51b37125 100644 --- a/.env +++ b/.env @@ -3,12 +3,24 @@ # 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, host, port, and database name as applicable. # Format: protocol://username:password@host:port/databaseName - DATABASE_URL= + +# 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= diff --git a/.gitignore b/.gitignore index 72eae3c0..d1bc7cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,49 @@ -.vscode -/node_modules +# 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 ee8ef98a..bd663fdc 100644 --- a/package.json +++ b/package.json @@ -9,25 +9,26 @@ "dev": "nodemon ./src/app.js" }, "dependencies": { - "@prisma/client": "^5.9.1", + "@prisma/client": "^5.13.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.1", - "express": "^4.18.2", + "dotenv": "^16.4.5", + "express": "^4.19.2", "express-async-handler": "^1.2.0", - "express-rate-limit": "^7.1.5", + "express-rate-limit": "^7.2.0", "helmet": "^7.1.0", - "joi": "^17.12.1", + "joi": "^17.13.1", "jsonwebtoken": "^9.0.2", "morgan": "^1.10.0", - "nodemailer": "^6.9.9", + "nodemailer": "^6.9.13", "uuidv4": "^6.2.13" }, "devDependencies": { - "yonode": "^1.0.0", - "nodemon": "^3.0.1", - "prisma": "^5.9.1" + "yonode": "^1.2.3", + "nodemon": "^3.1.0", + "prisma": "^5.13.0" } } diff --git a/src/app.js b/src/app.js index d02ae925..0186190a 100644 --- a/src/app.js +++ b/src/app.js @@ -1,17 +1,57 @@ // 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 { port } from "./config/initialConfig.js"; +import helloWorldRouter from "./routes/helloWorldRoutes.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, () => { console.log(`${chalk.green.bold("Server")} is listening on port ${port}`); -}); +}); \ No newline at end of file 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 6b230749..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..107fb387 --- /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", + }); + } + }; \ No newline at end of file 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 2c054c51..00000000 --- a/src/routes/router.js +++ /dev/null @@ -1,5 +0,0 @@ -import express from "express"; - -const routerName = express.Router(); - -export default routerName;