[GoLUG] React Presentation tonight 11/6/2024 7pm Eastern Standard time
Ron / BCLUG
admin at bclug.ca
Fri Nov 8 05:58:00 EST 2024
Steve Litt wrote on 2024-11-06 12:44:
> If you've ever wanted to learn more about React.js, or just be
> introduced to it
There was a question during the presentation that I'd like to follow up on.
Steve asked if there was something like "useContext" React hook in Node.js.
useContext is for when one wants to pass some variable / functions from
one component down through many "children" components, say 5 levels deep.
Instead of passing from A to B to C to D to E, where A's functions /
variables are used, useContext in E can directly access what A Provides
via context.
In Node.js, one can use the request.locals for something similar when
sharing variables between middleware functions.
So, say a user uploads a file via HTTP "post" method to Node, and 3
things happen: validate user, accept file, process file:
const router = express.Router();
const uploadFile = router.post("/upload",
AuthUser,
SaveFile,
ProcessFile,
);
const AuthUser = (request,response,next) => {
const { user, jwt_token } = request.body;
...validate user... save the users storage folder:
request.locals.userFolder = userFolder;
next() // move on to next middleware function
};
const SaveFile = (req,res,next) => {
const file_location = req.locals.userFolder;
... do stuff ...
next();
};
We can see that req.locals object can be a storage area accessible by
each function on the server, and each function only needs to accept some
combination of request object, response object, and next function.
Thanks to all who attended.
rb
More information about the GoLUG
mailing list