-
-
Notifications
You must be signed in to change notification settings - Fork 8
Closed
Description
💬 Questions and Help
I do want to include Reuest Id in the headers, so when users will have a problem with API call, we can easily track it back.
ATM we have super simple code:
import 'reflect-metadata';
import { config as dotEnvConfig } from 'dotenv';
import fastify from 'fastify';
import { Server, IncomingMessage, ServerResponse } from 'http';
import { genReqId } from './helpers/generateReqId';
// define server and its config
const server: fastify.FastifyInstance<Server, IncomingMessage, ServerResponse> = fastify({
logger: true,
genReqId
});
// load env variables and do the configuration
dotEnvConfig();
const serverPort: number = process.env.PORT ? parseInt(process.env.PORT) : 3000;
server.decorate('config', process.env);
// set plugins
server.register(require('fastify-cors'));
server.register(require('fastify-favicon'));
server.get('/ping', async (request, reply) => {
request.user = {
id: 1,
name: 'mac'
};
reply.send(request.user);
});
server.listen(serverPort, function(err, address) {
if (err) {
server.log.error(err);
process.exit(1);
}
});genrateRequestId.ts
import hyperId from 'hyperid';
const hId = hyperId({
urlSafe: true
});
export const genReqId = () => hId();Code works fine. Logging to console works fine (I am using HyperID).
However I do not see any RequestID header. I tried to change the header name, but it did not help...
{"level":30,"time":1573505563036,"pid":38117,"hostname":"Macs-MacBook-Pro-2.local","reqId":"kJ-gvC7vRzaqv_npgZzoSg-7","req":{"method":"GET","url":"/ping","hostname":"127.0.0.1:4000","remoteAddress":"127.0.0.1","remotePort":61811},"msg":"incoming request","v":1}
{"level":30,"time":1573505563036,"pid":38117,"hostname":"Macs-MacBook-Pro-2.local","reqId":"kJ-gvC7vRzaqv_npgZzoSg-7","res":{"statusCode":200},"responseTime":0.1978759989142418,"msg":"request completed","v":1}
➜ http GET http://127.0.0.1:4000/ping
HTTP/1.1 200 OK
Connection: keep-alive
Date: Tue, 12 Nov 2019 11:09:22 GMT
access-control-allow-origin: *
content-length: 21
content-type: application/json; charset=utf-8
{
"id": 1,
"name": "mac"
}
Any idea what I am doing wrong? Reading the manual I understood that it should work out of the box...
- Node v10.16.3 via NVM
- Mac OS 10.14.4 (18E226)
- Fastify v2.10.0
Metadata
Metadata
Assignees
Labels
No labels