Skip to content

Commit

Permalink
feat: added ipMiddeleware
Browse files Browse the repository at this point in the history
  • Loading branch information
esmaaksoy committed May 4, 2024
1 parent 100a6a7 commit e815596
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const app = express()
const cors = require("cors");
app.use(cors({ origin: "https://cactusink-esma.netlify.app" }));


require('dotenv').config()
const HOST = process.env?.HOST || '127.0.0.1'
const PORT = process.env?.PORT || 8000
Expand All @@ -27,7 +28,7 @@ app.use('/upload', express.static('./upload'))
app.use(require('./src/middlewares/authentication'))
// app.use(require('./src/middlewares/logger'))
app.use(require('./src/middlewares/queryHandler'))

app.use(require("./src/middlewares/ipMiddleware"));


//! Routes
Expand Down
10 changes: 9 additions & 1 deletion package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"keywords": [],
"author": "Esma Aksoy",
"license": "ISC Licence",


"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.3.1",
Expand All @@ -23,6 +21,7 @@
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.7",
"redoc-express": "^2.1.0",
"request-ip": "^3.3.0",
"swagger-autogen": "^2.23.7",
"swagger-ui-express": "^5.0.0"
}
Expand Down
7 changes: 4 additions & 3 deletions src/controllers/blog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const Blog = require("../models/blog");
const ipMiddleware = require("../middlewares/ipMiddleware");

module.exports = {
list: async (req, res) => {
Expand Down Expand Up @@ -83,19 +84,19 @@ module.exports = {
#swagger.summary = "Get Single Blog"
*/

const IP = req.ip;
const ip = req.clientIp;

const data = await Blog.findOneAndUpdate(
{ _id: req.params.id },
// { $inc: { countOfVisitors: 1 } }, //! not unique
{ $addToSet: { visitors: IP } }, //!unique count
{ $addToSet: { visitors: ip } }, //!unique count
{ new: true }
).populate([{ path: "comments", populate: "userId" }, "userId"]);

res.status(200).send({
error: false,
data,
});

},

update: async (req, res) => {
Expand Down
7 changes: 7 additions & 0 deletions src/middlewares/ipMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const requestIp = require("request-ip");

module.exports = (req, res, next) => {
const ip = requestIp.getClientIp(req);
req.clientIp = ip
next();
};
2 changes: 2 additions & 0 deletions src/models/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ const blogSchema = new mongoose.Schema(
ref: "Comment",
},
],

visitors: [],

countOfVisitors: {
type: Number,
get: function () {
Expand Down

0 comments on commit e815596

Please sign in to comment.