@akrdevtech/lib-express-request-id
v1.0.0
Published
Request/ Transaction id generator for Express Js
Downloads
27
Maintainers
Readme
Express Request/Transaction Id Middleware
Description
Append transaction id to every request to the express server application. We can specify the existing id header or populate a new transaction id.
- TypeScript support.
- Populate id from existing header or generate a random uuid
- Setup a custom response header to pipe the transaction id to the requester
- Pass in any custom logic to generate a transaction id of your own
Quick Links
Usage
Install
npm i @akrdevtech/lib-express-request-id
Example Usage
import * as express from 'express'
import { expressRequestId } from '@akrdevtech/lib-express-request-id';
const app = express();
app.use(expressRequestId());
app.get('/', [
(req, res) => {
res.send(`Hello World! ${req.txId}`) // Hello World! 44bd3872-f16c-46fc-a775-97d83c796432
}
]);
const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);
Custom Id Generator Usage Example
import * as express from 'express'
import { expressRequestId } from '@akrdevtech/lib-express-request-id';
const app = express()
const customIdGen = (req: express.Request): string => {
const id:string = // your random id generator logic here
return id
}
app.use(expressRequestId({ customIdGenerator: customIdGen ));
app.get('/', [
(req, res) => {
res.send(`Hello World! ${req.txId}`)
}
]);
const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);
API
expressRequestId(options?)
Options contains the following attributes
requestHeaderName
- Defines name of header, that should be used for checking and setting request ID.- Type:
string
- Default:
"x-transaction-id"
- Type:
responseHeaderName
- Defines name of header, that should be used for setting the id in reponse.- Type:
string
- Default:
"x-transaction-id"
- Type:
setResponseHeader
- Specifies whether the transaction id is to be set in the response header- Type:
boolean
- Default:
true
- Type:
attributeName
- Specifies the attribute name through which we can access the id from request object- Type:
string
- Default:
'txId'
- Type:
customIdGenerator
- Defines a custom method that takes in optional attribute as request object and returns an id string- Type:
Function
- Interface:
(request?: Express.Request)=>string
- Default:
(request?: Express.Request)=>string(UUID v4)
- Type: