-
Notifications
You must be signed in to change notification settings - Fork 13
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
saura8668
wants to merge
10
commits into
idna001:master
Choose a base branch
from
saura8668:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e8e0f3f
07-10-2023
saura8668 96f3edc
07-10-2023
saura8668 1ccf3ad
Merge branch 'master' into master
idna001 d3a131f
07-10-2023
saura8668 8cc132a
Merge branch 'master' of https://github.com/saura8668/Couple-Memory
saura8668 a4f301f
07-10-2023
saura8668 9fcbf2a
07-10-2023
saura8668 634fda6
08-10-2023
saura8668 c9758ce
Merge branch 'master' into master
idna001 156f1d3
Update src/App.js
idna001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
/node_modules | ||
/build | ||
/.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
const connectDB = async () => { | ||
try { | ||
const conn = await mongoose.connect(mongoURI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}); | ||
console.log(`MongoDB Connected ${conn.connection.host}-${conn.connection.port}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dev only please |
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.