Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix site view counter #34

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions Couple-Memory_backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/node_modules
/build
/.env
16 changes: 16 additions & 0 deletions Couple-Memory_backend/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('dotenv').config(); // Load environment variables from .env file
const mongoose = require('mongoose');
const mongoURI = `mongodb+srv://saura8668:${process.env.USER_KEY}@cluster0.c48azqk.mongodb.net/viewcount`;
Copy link
Owner

Choose a reason for hiding this comment

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

I think here is missing the .env File

Isnt it possible to run mongo db on this same server?
If not, I rather configure this.

const connectDB = async () => {
try {
const conn = await mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log(`MongoDB Connected ${conn.connection.host}-${conn.connection.port}`);
Copy link
Owner

Choose a reason for hiding this comment

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

This is more a dev only thing, don't want to show in prod

} catch (error) {
console.error(`Error connecting to MongoDB: ${error.message}`);
}
};

module.exports = connectDB;
27 changes: 27 additions & 0 deletions Couple-Memory_backend/controllers/viewCountController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const asyncHandler = require('express-async-handler')
const ViewCounter = require('../models/viewCountModel')

// @desc Count site visits
// @route PUT /site/count
// @access Public
const viewCount = asyncHandler(async (req, res) => {
//check if the counter is already created
let data = await ViewCounter.find({})
if (data.length == 0) {
let initCounter = await ViewCounter.create({
counter: 1,
})
res.status(200).json({ "view": initCounter })
}
else {
data[0].counter += 1
data[0].save()
res.status(200).json({ "view": data[0] })
}

})

module.exports = {
viewCount
}

19 changes: 19 additions & 0 deletions Couple-Memory_backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express')
const app = express()
const cors = require('cors');


const connectDB = require('./config/db')
connectDB();

app.use(cors());
// Available routes
app.use(express.json())
app.use(express.urlencoded({ extended: false }))

app.use('/site', require('./routes/viewCountRoute'))

const PORT = 3030
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
Copy link
Owner

Choose a reason for hiding this comment

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

Dev only please

})
9 changes: 9 additions & 0 deletions Couple-Memory_backend/models/viewCountModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mongoose = require('mongoose')

const viewCountSchema = mongoose.Schema({
counter:{
type: Number
}
},{timestamps:true})

module.exports = mongoose.model('ViewCounter',viewCountSchema)
Loading