Skip to content

Commit

Permalink
feat: changed JS-PostgreSQL-TypeORM-NoAuth-Template to the tempalte
Browse files Browse the repository at this point in the history
  • Loading branch information
abdinasir-Tman committed May 13, 2024
1 parent a5e1155 commit 5c21226
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 30 deletions.
17 changes: 14 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
48 changes: 47 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
46 changes: 42 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -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, () => {
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 "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({
Expand All @@ -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;
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/helloWorldRoutes.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 5c21226

Please sign in to comment.