@aaxis/express-request-id 中文文档教程

发布于 5年前 浏览 22 项目主页 更新于 3年前

express-request-id NPM versionBuild Status

为请求生成 UUID 并将其添加到 X-Request-Id 标头。 如果请求包含 X-Request-Id 标头,则使用其值。

var app = require('express')();
var addRequestId = require('express-request-id')();

app.use(addRequestId);

app.get('/', function (req, res, next) {
    res.send(req.id);
    next();
});

app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});

// curl localhost:3000
// d7c32387-3feb-452b-8df1-2d8338b3ea22

API

express-request-id([options])

返回中间件函数,它将请求 id 附加到 req 对象。

options

  • uuidVersion - version of uuid to use (defaults to v4). Can be one of methods from node-uuid.
  • setHeader - boolean, indicates that header should be added to response (defaults to true).
  • setReqHeader - boolean, indicates that header should be added to request (defaults to true).
  • headerName - string, indicates the header name to use (defaults to X-Request-Id).
  • attributeName - string, indicates the attribute name used for the identifier on the request object (defaults to id)

此选项字段直接传递给 node-uuid 函数:

  • Whole options object, that can contain fields like: node, clockseq, msecs, nsecs.
  • options.buffer and options.offset to uuid function as second and third parameters.

License

MIT (c) 2020 Derek Hu (derekhu@aaxiscommerce.com)

express-request-id NPM versionBuild Status

Generate UUID for request and add it to X-Request-Id header. In case request contains X-Request-Id header, uses its value instead.

var app = require('express')();
var addRequestId = require('express-request-id')();

app.use(addRequestId);

app.get('/', function (req, res, next) {
    res.send(req.id);
    next();
});

app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});

// curl localhost:3000
// d7c32387-3feb-452b-8df1-2d8338b3ea22

API

express-request-id([options])

Returns middleware function, that appends request id to req object.

options

  • uuidVersion - version of uuid to use (defaults to v4). Can be one of methods from node-uuid.
  • setHeader - boolean, indicates that header should be added to response (defaults to true).
  • setReqHeader - boolean, indicates that header should be added to request (defaults to true).
  • headerName - string, indicates the header name to use (defaults to X-Request-Id).
  • attributeName - string, indicates the attribute name used for the identifier on the request object (defaults to id)

This options fields are passed to node-uuid functions directly:

  • Whole options object, that can contain fields like: node, clockseq, msecs, nsecs.
  • options.buffer and options.offset to uuid function as second and third parameters.

License

MIT (c) 2020 Derek Hu (derekhu@aaxiscommerce.com)

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文