Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a request property instead of global var for tenantId #72

Open
pjoe opened this issue Nov 18, 2022 · 1 comment
Open

Use a request property instead of global var for tenantId #72

pjoe opened this issue Nov 18, 2022 · 1 comment

Comments

@pjoe
Copy link

pjoe commented Nov 18, 2022

In Lab2/Part3 a global var is used to store tenantId for incoming requests. Not sure how that would work with multiple requests in flight.

var tenantId = '';
// Configure middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
bearerToken = req.get('Authorization');
if (bearerToken) {
tenantId = tokenManager.getTenantId(req);
}
next();
});

A better way would be to store this on the request object, see example: https://expressjs.com/en/guide/writing-middleware.html (under "Middleware function requestTime")

@brtrvn
Copy link
Contributor

brtrvn commented May 16, 2023

NodeJS is a single threaded system - so every request gets lives in its own scope. There wouldn't be a way to leak across requests. That said, if it makes more sense to pass the tenant id through mutating the incoming request object, there's no harm that I can see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants