mirror of
				https://github.com/goldbergyoni/nodebestpractices.git
				synced 2025-11-01 01:56:06 +08:00 
			
		
		
		
	http request example
This commit is contained in:
		| @ -25,13 +25,11 @@ app.get('/getUserData/{id}', async (req, res, next) => { | |||||||
|  |  | ||||||
|         // At any point in the app after cls-rtracer middleware was initialized, even when 'req' object doesn't exist, the TransactionId is reachable |         // At any point in the app after cls-rtracer middleware was initialized, even when 'req' object doesn't exist, the TransactionId is reachable | ||||||
|         const transactionId = rTracer.id(); |         const transactionId = rTracer.id(); | ||||||
|  |  | ||||||
|         logger.info(`user ${user.id} data was fetched successfully`, { transactionId }); |         logger.info(`user ${user.id} data was fetched successfully`, { transactionId }); | ||||||
|  |  | ||||||
|         res.json(user); |         res.json(user); | ||||||
|     } catch (err) { |     } catch (err) { | ||||||
|  |         // If not for the example, I'd recommand using rTracer.id() from inside the logger component, to prevent from using it all over the code | ||||||
|         // As next step I'd recommand using rTracer.id() from inside the logger component, to prevent from using it all over the code |  | ||||||
|         logger.error(`error while fetching user id ${req.params.id} data`, { transactionId: rTracer.id(), error: err }); |         logger.error(`error while fetching user id ${req.params.id} data`, { transactionId: rTracer.id(), error: err }); | ||||||
|  |  | ||||||
|         next(err); |         next(err); | ||||||
| @ -53,10 +51,14 @@ app.use(rTracer.expressMiddleware({ | |||||||
|     headerName: 'x-transaction-id' |     headerName: 'x-transaction-id' | ||||||
| })); | })); | ||||||
|  |  | ||||||
|  | const axios = require("axios"); | ||||||
|  |  | ||||||
|  | // Now, the external service will automaticlly get the current TransactionId as header | ||||||
|  | const response = await axios.get('https://externalService.com/api/getAllUsers'); | ||||||
| ``` | ``` | ||||||
| <br/><br/> | <br/><br/> | ||||||
|  |  | ||||||
| ### Good: Logs with an assigned TransactionId - can be used it as filter to see only a single flow | ### Good: Logs with an assigned TransactionId - can be used as filter to see only a single flow | ||||||
|  |  | ||||||
| <br/><br/> | <br/><br/> | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Yedidya
					Yedidya