- 快速入门
- 指南
- 参考手册
- 高级主题
- 资源
- Express 词汇表
- Express 社区
- 模板引擎
- Express 中间件
- Express body-parser middleware
- Express compression middleware
- Express connect-rid middleware
- Express cookie-parser middleware
- Express cookie-session middleware
- Express cors middleware
- Express csurf middleware
- Express errorhandler middleware
- Express method-override middleware
- Express morgan middleware
- Express multer middleware
- Express response-time middleware
- Express serve-favicon middleware
- Express serve-index middleware
- Express serve-static middleware
- Express session middleware
- Express timeout middleware
- Express vhost middleware
- Express 实用模板
- 推荐框架
- 推荐书籍和博客
Express response-time middleware
Response time for Node.js servers.
This module creates a middleware that records the response time for requests in HTTP servers. The “response time” is defined here as the elapsed time from when a request enters this middleware to when the headers are written out to the client.
Installation
This is a Node.js module available through the npm registry. Installation is done using the npm install
command:
$ npm install response-time
API
var responseTime = require('response-time')
responseTime([options])
Create a middleware that adds a X-Response-Time
header to responses. If you don’t want to use this module to automatically set a header, please see the section about responseTime(fn)
.
Options
The responseTime
function accepts an optional options
object that may contain any of the following keys:
digits
The fixed number of digits to include in the output, which is always in milliseconds, defaults to 3
(ex: 2.300ms
).
header
The name of the header to set, defaults to X-Response-Time
.
suffix
Boolean to indicate if units of measurement suffix should be added to the output, defaults to true
(ex: 2.300ms
vs 2.300
).
responseTime(fn)
Create a new middleware that records the response time of a request and makes this available to your own function fn
. The fn
argument will be invoked as fn(req, res, time)
, where time
is a number in milliseconds.
Examples
express/connect
var express = require('express')
var responseTime = require('response-time')
var app = express()
app.use(responseTime())
app.get('/', function (req, res) {
res.send('hello, world!')
})
vanilla http server
var finalhandler = require('finalhandler')
var http = require('http')
var responseTime = require('response-time')
// create "middleware"
var _responseTime = responseTime()
http.createServer(function (req, res) {
var done = finalhandler(req, res)
_responseTime(req, res, function (err) {
if (err) return done(err)
// respond to request
res.setHeader('content-type', 'text/plain')
res.end('hello, world!')
})
})
response time metrics
var express = require('express')
var responseTime = require('response-time')
var StatsD = require('node-statsd')
var app = express()
var stats = new StatsD()
stats.socket.on('error', function (error) {
console.error(error.stack)
})
app.use(responseTime(function (req, res, time) {
var stat = (req.method + req.url).toLowerCase()
.replace(/[:.]/g, '')
.replace(/\//g, '_')
stats.timing(stat, time)
}))
app.get('/', function (req, res) {
res.send('hello, world!')
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论