Javascript 如何使用Node.js/Express.js和Mongodb统计API调用
我想统计所有API调用并更新到mongodb中。请在下面找到我的代码,我正在使用Express.js mongoose,在这段代码中,我从mongodb获取我的APIUser信息,并验证authorize user,然后提交APICall文件 当API用户以较低的速率(例如10-20req/sec)发送请求时,此代码工作正常 但在实际场景中,我的请求量越来越大,例如1000req/秒 是否有任何方法可以准确计算我的API调用 请用您的mongodb url替换process.env.MONGOURI app.js-Javascript 如何使用Node.js/Express.js和Mongodb统计API调用,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我想统计所有API调用并更新到mongodb中。请在下面找到我的代码,我正在使用Express.js mongoose,在这段代码中,我从mongodb获取我的APIUser信息,并验证authorize user,然后提交APICall文件 当API用户以较低的速率(例如10-20req/sec)发送请求时,此代码工作正常 但在实际场景中,我的请求量越来越大,例如1000req/秒 是否有任何方法可以准确计算我的API调用 请用您的mongodb url替换process.env.MONGOU
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
)
);
用户模式-
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
}
});
你能提供一个简单的工作示例吗?这非常有助于在代码中找到问题。我已经更新了我的查询。