在NodeJs中存储最后一次接收REST调用的日期/时间
Store date/time of last received REST call in NodeJs
我已经在NodeJs Connect-rest中编写了一个REST API,我想知道如何存储最后一次调用的日期和时间。
这里是GET url调用
http://localhost:8080/api/books/metadata?id=12
过滤id
的代码片段module.exports.getItem = function(callback, id) {
var searchLetter = new RegExp(id, 'i');
//would like to record date/time the last time this call was made to this ID/row
//return the date/time back in the callback method
var row = items.filter(function(itemList) {
return searchLetter.test(itemList.asname);
});
return callback(null, {items: row, matches: row.length}, {headers: {'Content-type': 'application/json'}});
};
你可以添加一个'global route',以便在你的服务器设置时(也就是说,如果你使用Express或restify或类似的)使用任何api调用:
app.use(function (req, res, next) {
console.log( "Got a request at " + new Date() ); //might want to format date
});
(request参数可能已经存储了时间)
其中一些框架将有一个"after"事件,它在请求被完全处理后被触发(所以这个日志记录不是链的真正一部分)。就像这个来自restify的例子,我构建了一个自定义的日志消息:server.on( 'after', function logRequest( req, res, route, error ) {
var latency = res.get( 'Response-Time' );
if ( typeof( latency ) !== 'number' )
latency = Date.now() - req._time;
var obj = {
userAgent: req.headers[ 'user-agent' ],
url: req.url,
method: req.method,
statusCode: res.statusCode,
req_id: req.getId()
};
obj.latency = latency;
server.log.info( obj );
} );
在这个例子中,我使用的是restify的日志中间件,但是你也可以使用你自己的日志记录器,console.log
,追加到文件等
相关文章:
- 在CouchDB Map函数中发出日期时-对日期对象调用的内容
- 如何调用日期函数并在接下来的 6 天插槽中显示
- jquery在动态元素上调用日期选择器
- 查询日期时间选择器未在另一个函数调用中获取日期
- 当在运行nodejs的服务器上调用new Date()时,Date将返回服务器启动时的日期
- JS根据AJAX调用传递的值创建日期对象
- 如何为引导日期选择器调用javascript函数
- 如何在日期选择器 jquery 的 Select 事件上调用 js 函数
- 通过函数调用 JQuery UI 日期选择器
- 在 ajax 调用中加载日期选择器,然后单击不起作用
- 初始化引导日期选择器后调用 noConflict()
- 将日期参数传递给 MVC 操作的 ajax 调用的安全方法
- 流星:如何让 JS 日期范围选择器调整 API 调用日期参数
- Jquery ajax 调用从日期选择器上选择消失文本框
- 如何在引导程序日期选择器上调用更改事件
- 当从客户端从ajax调用检索时,我在服务器上的日期时间增加了12.30小时
- 多次调用日期,返回相同的结果
- 当使用类似的参数调用日期构造函数时,会提供意外的结果
- 如何使用angularjs在页面加载中调用日期选择器Jquery插件
- 如何为多个输入元素调用日期范围选择器