### Git Commands for Project Contribution Setup Source: https://github.com/skonves/express-http-context/blob/master/CONTRIBUTING.md These Git commands are essential for setting up your local development environment when contributing to the express-http-context project. They cover cloning the repository and creating a new feature branch. ```Shell git clone https://github.com/your-GitHub-username/express-http-context.git ``` ```Shell git checkout -b branch-name-here ``` -------------------------------- ### Install and Initialize Express HTTP Context Middleware Source: https://github.com/skonves/express-http-context/blob/master/README.md This snippet demonstrates how to install the `express-http-context` package via npm and integrate its middleware into an Express.js application. The middleware should be applied early in the application's setup to ensure all subsequent code has access to the request-scoped context. ```shell npm i express-http-context ``` ```javascript import express from 'express'; import * as httpContext from 'express-http-context'; const app = express(); app.use(httpContext.middleware); // All code from here on has access to the same context for each request ``` -------------------------------- ### Install Legacy Express HTTP Context Versions Source: https://github.com/skonves/express-http-context/blob/master/README.md These commands provide instructions for installing specific legacy versions of `express-http-context` compatible with older Node.js environments. Version 0 is for Node.js versions less than 7, and version 1 is for Node.js versions 8 through 11. ```shell npm install --save express-http-context@0 ``` ```shell npm install --save express-http-context@1 ``` -------------------------------- ### Set Request-Scoped Context Values in Express Middleware Source: https://github.com/skonves/express-http-context/blob/master/README.md This example illustrates how to set values within the request-scoped context using `httpContext.set()`. It shows an authentication middleware that retrieves user data from the request and stores it in the context, making it accessible to other parts of the application during the same request lifecycle. ```javascript // Example authentication middleware app.use(async (req, res, next) => { try { // Get user from data on request const bearer = req.get('Authorization'); const user = await userService.getUser(bearer); // Add user to the request-scoped context httpContext.set('user', user); return next(); } catch (err) { return next(err); } }); ``` -------------------------------- ### Publishing to NPM Source: https://github.com/skonves/express-http-context/blob/master/packages/express-http-context-intermediate-library/README.md This command demonstrates how to publish the library to the NPM registry using the provided shell script. It requires a semantic version number as an argument. ```shell ./npmPublish.sh a.b.c ``` -------------------------------- ### Retrieve Request-Scoped Context Values Source: https://github.com/skonves/express-http-context/blob/master/README.md This snippet demonstrates how to retrieve previously set values from the request-scoped context using `httpContext.get()`. It highlights the utility of `express-http-context` by showing how data, like a user object, can be accessed deep within service logic without direct access to the Express `req` object. ```javascript import * as httpContext from 'express-http-context'; // Somewhere deep in the Todo Service async function createTodoItem(title, content) { // Get the user from the request-scoped context const user = httpContext.get('user'); await db.insert({ title, content, userId: user.id }); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.