簡體   English   中英

如何使用 Node.js/Express.js 和 Mongodb 計算 API 調用

[英]How to Count API Calls using Node.js/Express.js and Mongodb

我想計算我所有的 API 調用並更新到我的 mongodb 中。 請在我的代碼下方找到我使用 Express.js mongoose ,在這段代碼中,我從 mongodb 獲取我的 APIUser 信息並驗證授權用戶,然后增加 APICall 提交。

當 API 用戶以較低的速率發送請求時,此代碼工作正常,例如。 10-20請求/秒

但在實際場景中,我對請求越來越多,例如。 1000 請求/秒。

有什么方法可以准確地計算我的 API 調用。

請將 process.env.MONGOURI 替換為您的 mongodb 網址。

app.js -

const express = require('express');
const morgan = require('morgan');
const colors = require('colors');
const dotenv = require('dotenv');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

dotenv.config({ path: './config/config.env' });

const Employee = require('./models/EmployeeSchema');
const APIUser = require('./models/APIUserSchema');

 mongoose.connect(process.env.MONGOURI, {
      useNewUrlParser: true,
      useCreateIndex: true,
      useUnifiedTopology: true,
    });

const app = express();
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.post('/addemployee', async (req, res, next) => {
  var IP = req.header('X-Real-IP');
  const APIClientInfo = await APIUser.findOne({
    APIClientID: req.header('API-Client-ID'),
    APISecretKey: req.header('API-Secret-Key'),
  });
  //console.log(APIClientInfo);
  if (APIClientInfo) {
    try {
      //Copturaing API Request
      const { Name, PhoneNo, Age, Department, Salary } = req.body;
      const addemployee = await Employee.create(req.body);
      const Response = {
        Status: 'Success',
        Data: addemployee,
        Message: 'Successfully! Record has been inserted.',
      };
      
      APIClientInfo.APICalls++;
      await APIClientInfo.save();

      //Send Response
      res.status(201).json(Response);

      //Log

    } catch (err) {
      //if Valid Error Found
      if (err.name == 'ValidationError') {
        const messages = Object.values(err.errors).map((val) => val.message);
        const Response = {
          Error: {
            message: messages,
          },
        };
        res.status(400).json(Response);
  
      } else {
        const Response = {
          Error: {
            message: 'Internal Server Error',
          },
        };
        res.status(500).json(Response);
        //Send Error
  
      }
    }
  } else {
    //if API-Key is not valid
    res.status(401).json({
      Error: {
        message: 'Unauthorized',
      },
    });
  }
});

app.use((req, resp, next) => {
  resp.setHeader('Access-Control-Allow-Headers', '*');
  resp.setHeader('Access-Control-Allow-Origin', '*');
  resp.removeHeader('X-Powered-By', '*');
  resp.removeHeader('Server', '*');
  next();
});

// Error handling
app.use((req, resp, next) => {
  var error = new Error('Not Found ⛔ ');
  error.status = 404;
  next(error);
});

app.use((error, req, resp, next) => {
  resp.status(error.status || 500);
  resp.json({
    Error: {
      message: error.message,
    },
  });
});

//console.log = function(){};

const PORT = process.env.PORT || 5000;

app.listen(
  PORT,
  console.log(
    `Server Started in ${process.env.NODE_ENV} mode on Port ${PORT}`.white.bold
  )
);

API用戶架構 -

const mongoose = require('mongoose');
const uuid = require('uuid');
const moment = require('moment-timezone');

const UserSchema = new mongoose.Schema({
  _id: {
    type: String,
    default: uuid.v4,
  },
  Username: {
    type: String,
    index: { unique: true },
    required: [true, 'Please Enter Your UserName'],
  },
  Password: {
    type: String,
    required: [true, 'Please Enter Your Password'],
  },
  Email: {
    type: String,
    index: { unique: true },
    required: [true, 'Please Enter Your Email ID'],
  },
  APIClientID: {
    type: String,
    index: { unique: true },
    minlength: 10,
    maxlength: 40,
    default: uuid.v4,
  },
  APISecretKey: {
    type: String,
    index: { unique: true },
    minlength: 10,
    maxlength: 40,
    default: uuid.v4,
  },
  APICalls: {
    type: Number,
    default: 0,
  },
  CreatedAt: {
    type: String,
    default: function () {
      return moment().tz('Asia/Kolkata').format('MMMM Do YYYY, hh:mm:ss A');
    },
  },
  ModifiedAt: {
    type: String,
    default: function () {
      return moment().tz('Asia/Kolkata').format('MMMM Do YYYY, hh:mm:ss A');
    },
  },
});

UserSchema.set('toJSON', {
  transform: function (doc, ret, options) {
    ret.UserRefNo = ret._id;
    delete ret._id;
    delete ret.__v;
  },
});

module.exports = mongoose.model('APIUser', UserSchema);

EmployeeSchema.js -

const mongoose = require('mongoose');
const uuid = require('uuid');
const moment = require('moment-timezone');

const EmployeeSchema = new mongoose.Schema({
  _id: {
    type: String,
    default: uuid.v4,
  },
  Name: {
    type: String,
    trim: true,
    required: [true, 'Please Enter Your Name'],
  },
  PhoneNo: {
    type: String,
    trim: true,
    required: [true, 'Please Enter Your Phone No'],
  },
  Age: {
    type: String,
    required: [true, 'Please Enter Your Employee Age'],
  },
  Department: {
    type: String,
    trim: true,
    required: [true, 'Please Enter Your Department Name'],
  },
  Salary: {
    type: String,
    required: [true, 'Please Enter Your Employee Salary PA'],
  },
  CreatedAt: {
    type: String,
    default: function () {
      return moment().tz('Asia/Kolkata').format('MMMM Do YYYY, hh:mm:ss A');
    },
  },
  ModifiedAt: {
    type: String,
    default: function () {
      return moment().tz('Asia/Kolkata').format('MMMM Do YYYY, hh:mm:ss A');
    },
  },
});

EmployeeSchema.set('toJSON', {
  transform: function (doc, ret, options) {
    ret.EmpRefNo = ret._id;
    delete ret._id;
    delete ret.__v;
  },
});

module.exports = mongoose.model('Employee', EmployeeSchema);

嘗試使用updateOne的單個函數更新 api 調用值:

 await APIUser.updateOne({
    APIClientID: req.header('API-Client-ID'),
    APISecretKey: req.header('API-Secret-Key'),
  }, {
    $inc: {
      APICalls: 1
    }
  });
 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM