Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions Backend/config/db.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
const mongoose = require("mongoose");
require("dotenv").config();

const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
// We are adding a 'tls' option here.
// This is a common workaround for the 'tlsv1 alert internal error'
// that can happen on some networks or computers.
const conn = await mongoose.connect(process.env.MONGO_URI, {
tls: true,
tlsAllowInvalidCertificates: true, // Use this for development only
Comment on lines +9 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Security concern: TLS certificate validation disabled.

The tlsAllowInvalidCertificates: true option disables certificate validation, which poses a security risk. While acceptable for development, ensure this is not used in production environments.

Consider using environment-based configuration:

-      tls: true,
-      tlsAllowInvalidCertificates: true, // Use this for development only
+      tls: true,
+      tlsAllowInvalidCertificates: process.env.NODE_ENV === 'development'
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
tls: true,
tlsAllowInvalidCertificates: true, // Use this for development only
tls: true,
tlsAllowInvalidCertificates: process.env.NODE_ENV === 'development'
πŸ€– Prompt for AI Agents
In Backend/config/db.js around lines 9 to 10, the option
tlsAllowInvalidCertificates is set to true, disabling TLS certificate validation
and posing a security risk. Modify the code to conditionally set
tlsAllowInvalidCertificates based on the environment, enabling it only in
development and disabling it in production by using an environment variable
check (e.g., process.env.NODE_ENV). This ensures secure TLS validation in
production while allowing flexibility during development.

});
console.log("MongoDB Connected");

console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.error(error);
process.exit(1);
console.error(`Error connecting to MongoDB: ${error.message}`);
process.exit(1); // Exit process with failure
}
};

// The original file likely calls the function to connect immediately.
// We will keep that structure.
connectDB();
module.exports = mongoose;

// We don't strictly need to export, but it's good practice.
module.exports = connectDB;
16 changes: 16 additions & 0 deletions Backend/controllers/visitorController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Visitor = require('../models/Visitor');

// Increments visitor count and returns it
exports.getAndIncrementVisitorCount = async (req, res) => {
try {
const stats = await Visitor.findOneAndUpdate(
{ name: 'site_stats' }, // Filter to find the document
{ $inc: { count: 1 } }, // Increment the 'count' field by 1
{ new: true, upsert: true } // Options: return the updated doc, and create it if it doesn't exist
);
res.status(200).json({ count: stats.count });
} catch (error) {
console.error('Error updating visitor count:', error);
res.status(500).json({ message: 'Server error' });
}
};
14 changes: 11 additions & 3 deletions Backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ require("dotenv").config();
const bodyParser = require("body-parser");
require("./config/db");

// 1. Import the new visitor routes
const visitorRoutes = require('./routes/visitorRoutes');

const app = express();
app.use(bodyParser.json({ limit: "50mb" })); // Increase JSON request size limit
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(bodyParser.json({ limit: "50mb" })); // Increase JSON request size limit
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(cors());
app.use(express.json());


app.use("/api", require("./routes/certificateRoutes"));
app.use("/api/images", require("./routes/imageRoutes"));
app.use("/auth", require("./routes/authRoutes"));

// 2. Add the new visitor route to the application
app.use("/api/visitors", visitorRoutes);

//404 handler
app.use((req, res, next) => {
res.status(404).json({ message: "Not Found" });
});

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

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
15 changes: 15 additions & 0 deletions Backend/models/Visitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require('mongoose');

const VisitorSchema = new mongoose.Schema({
name: {
type: String,
required: true,
default: 'site_stats'
},
count: {
type: Number,
default: 0
}
});

module.exports = mongoose.model('Visitor', VisitorSchema);
115 changes: 73 additions & 42 deletions Backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Backend/routes/visitorRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const router = express.Router();
const { getAndIncrementVisitorCount } = require('../controllers/visitorController');

// GET /api/visitors/count
router.get('/count', getAndIncrementVisitorCount);

module.exports = router;
Loading