From ff9aa8ae4f98d40e449b3bd3145d217bad8de9f2 Mon Sep 17 00:00:00 2001 From: niftylettuce Date: Fri, 14 Jun 2019 03:28:02 -0500 Subject: [PATCH] feat: added express test and example output in README --- README.md | 2 +- src/index.js | 2 ++ test/basic.js | 14 +++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e4b0f81..fe7c5ce 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,7 @@ app.get('/', (req, res, next) => { [MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/) -## +## [npm]: https://www.npmjs.com/ diff --git a/src/index.js b/src/index.js index 20a67ad..5ea3da9 100644 --- a/src/index.js +++ b/src/index.js @@ -437,6 +437,8 @@ const parseRequest = (config = {}) => { result.request.timestamp = new Date(req[pinoHttpStartTime]).toISOString(); else if (req._startTime instanceof Date) result.request.timestamp = req._startTime.toISOString(); + else if (typeof req._startTime === 'number') + result.request.timestamp = new Date(req._startTime).toISOString(); // add `request.duration` if (typeof headers['X-Response-Time'] === 'string') diff --git a/test/basic.js b/test/basic.js index 0809a19..0d3426d 100644 --- a/test/basic.js +++ b/test/basic.js @@ -95,7 +95,7 @@ test('parses start time and start date as date', t => { t.true(typeof obj.request.duration === 'number'); }); -test('works with morgan req._startTime', t => { +test('works with morgan req._startTime date', t => { const req = {}; req._startTime = new Date(); req.headers = { @@ -107,6 +107,18 @@ test('works with morgan req._startTime', t => { t.true(typeof obj.request.duration === 'number'); }); +test('works with morgan req._startTime number', t => { + const req = {}; + req._startTime = Date.now(); + req.headers = { + 'X-Response-Time': '500 ms' + }; + const obj = parseRequest({ req }); + t.true(typeof obj.duration === 'number'); + t.true(typeof obj.request.timestamp === 'string'); + t.true(typeof obj.request.duration === 'number'); +}); + test('parses start time and start date as number', t => { const req = {}; req[startTime] = Date.now();