Skip to content

Commit

Permalink
feat: removed useNewUrlParser and useUnifiedTopology #348
Browse files Browse the repository at this point in the history
feat: removed useNewUrlParser and useUnifiedTopology
  • Loading branch information
sharafdin authored Apr 24, 2024
2 parents 520fb98 + 8dd4e1f commit 0dd33a0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// import the packages
import express from 'express';
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 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 helloWorldRouter from './routes/helloWorldRoutes.js';
import { port } from "./config/initialConfig.js";
import connectDB from "./config/dbConfig.js";
import helloWorldRouter from "./routes/helloWorldRoutes.js";

// Initializing the app
const app = express();
Expand All @@ -23,16 +23,16 @@ app.use(helmet());
app.use(cors());

// Logger middleware for development environment
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
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
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});
app.use(limiter);

Expand All @@ -43,17 +43,17 @@ app.use(express.json());
connectDB();

// Use your routes here
app.use('/api/helloworld', helloWorldRouter);
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: {}
});
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}`);
console.log(`${chalk.green.bold("Server")} is listening on port ${port}`);
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mongoose from "mongoose";
import { dbName, dbUrl } from "./initialConfig.js";
import chalk from "chalk";

const connectDB = async () => {
try {
await mongoose.connect(dbUrl, {
dbName,
});
console.log(`${chalk.green.bold("Connected")} to the database`);
} catch (error) {
console.log(`${chalk.red.bold("Error")} connecting to database`, error);
process.exit(1);
}
};

export default connectDB;

0 comments on commit 0dd33a0

Please sign in to comment.