简体   繁体   English

如何获取执行时间,总计数,ip 在nodejs api中命中的地址

[英]how to get execution time, total count, ip address who hit it in nodejs api

Basically what I want to do is to get a particular endpoint last execution time, total number of times api executed today and ip address of each machine who hits the api and the application response time of each time api hits.基本上我想要做的是获得一个特定的端点上次执行时间,今天执行的总次数 api 和每台机器的 ip 地址命中 api 和每次 api 命中的应用程序响应时间。 How to achieve this in nodeJS?如何在 nodeJS 中实现这一点? Basically it is rest api auditing system.基本上就是rest api审计系统。

There's nothing related specifically to Node here per se, you can log every request and the needed details somewhere (ie a plain database).这里本身没有任何与 Node 相关的内容,您可以在某处记录每个请求和所需的详细信息(即普通数据库)。 It might be handy to create a middleware that wraps all API endpoints (client -> middleware -> api endpoint function) that contains the logic for this.创建一个包含所有 API 端点(client -> middleware -> api endpoint function)的中间件可能很方便,其中包含此逻辑。

Since you're using NodeJS, you can create a callback mechanism for measuring response times, ie your middleware can provide a callback function to your API endpoint controller(s) that the controller invokes when it sends the response to the user (after res.send() if you're using Express).由于您使用的是 NodeJS,因此您可以创建一个回调机制来测量响应时间,即您的中间件可以向 API 端点控制器提供回调 function,controller 在向用户发送响应时调用该回调(在res.send()如果你使用的是 Express)。

I can answer some of your queries:-我可以回答您的一些问题:-

IP -> IP address of the machine that hits the api is present in the request object req.connection.remoteAddress in expressjs(It may not be proper if your request is being routed over a proxy like Akamai, in that case turn on True-Client-IP header in akamai) IP -> IP 命中 api 的机器地址存在于请求 object req.connection.remoteAddress in expressjs(如果您的请求是通过像 Akamai 这样的代理路由的,则可能不正确,在这种情况下打开 True-在 akamai 中的客户端 IP header)

Count -> make a db write every time a particular endpoint is requested.计数 -> 每次请求特定端点时进行数据库写入。 Just put it right before your entry point function(api) sends response.只需将其放在您的入口点函数 (api) 发送响应之前即可。

Response time of api - just put this at the start and end of your entry point function. At the start:- let start = process.hrtime();响应时间 api - 只需将其放在入口点 function 的开始和结束处。开始时:- let start = process.hrtime();

Before api sends response:- endTime = process.hrtime(start);在 api 发送响应之前:- endTime = process.hrtime(start); endTime = Math.round(((endTime[0] * 1000) + (endTime[1] / 1000000))); endTime = Math.round(((endTime[0] * 1000) + (endTime[1] / 1000000)));

Maybe you can wrap this in a function. It will give you the time elapsed in milliseconds and you can store that too somewhere for each iteration.也许你可以将它包装在 function 中。它会给你以毫秒为单位的经过时间,你也可以将它存储在每次迭代的某个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用中间件在nodejs中获取rest api的执行时间(获得响应的时间)和api - How to get the execution time(time to get response) and api of rest api in nodejs using middleware 使用Web API获取IP地址 - Get IP Address with Web API 如果两个设备同时在Node.js中命中api,如何将两个JSON对象都存储到mongodb - How to store both the JSON object to mongodb, if two device hit the api at same time in nodejs 如何使用Yahoo Weather API从IP地址获取客户端天气 - How to get client weather from IP Address with Yahoo Weather API 如何获取此 API 响应中的数据对象总数 - How do i get the total count of data objects in this API response 使用JavaScript通过IP地址获取用户本地时间? - Get user local time by ip address with JavaScript? 如何获取通过mandrill api发送打开我的邮件的所有电子邮件地址的列表 - How to get list of all email address who opened my mails sent through mandrill api 如何获取nodejs中缓冲区的地址? - How to get the address of the buffer in nodejs? NodeJS - 获取请求的真实IP地址,而不是Cloudflare的IP地址 - NodeJS - Get real ip address of a request instead of Cloudflare's ip address nodejs获取客户端IP setinterval里面的地址function - nodejs to get client IP Address inside setinterval function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM