How to Pass Data Through Middleware in Express JS
Вставка
- Опубліковано 12 лис 2024
- Learn how to pass data through middleware functions in Express JS with this quick and easy tutorial.
Resources:
-----------------
Source code: github.com/nax...
res.locals docs: expressjs.com/...
Code w/ Node: www.devsprout....
-----------------
Thumbnail Background Photo by Markus Spiske on Unsplash
This tutorial is wonderful. You really do a great job of explaining concepts and simplifying it even for beginners. Thank you!
I'm an Express beginner and this tutorial was wonderful! Great job explaining the concepts and thank you!
Thanks! I've been struggling with an Express project with relatively complex MVC structure and res.locals saved me!
saved my life man :)
i have a questuon.i have 2 route
FIRST
app.get('/user',(req,res){
req.user '' i want req.user to send second route
})
SECOND
app.put('/product'(req,res){
i want to use here
}
)
how can i do this
thannkss you saved my time
Awesome! 😊
Saved me. Thanks
Really great tutorial. Make more of these please!
Hey Ian, why for the Post.findById(req.params.id) you use 'id' and for the if statement post.author._id you use '_id'? What is the difference between id and _id? Thanks in advance.
_id is the format for mongoDB.
Really nice ! Was looking exactly for that :D
Wow! Thank you, sir. You and Traversy Media have been helpful.I've just subscribe to you.
thanks for video
thank you!!!
Np
awsm !
How would this relate to the "put" route of an edit where I currently still use the mongoose method "findByIdAndUpdate"?
You could edit the object properties directly, then save the updated object back into the database, thus eliminating the need to find and update with the findByIdAndUpdate method.
You can probably use the spread operator to update the found post object with the data from req.body (just a thought, it would need testing).
Hey Ian, I'm using express-handlebars for a project. Is there a way to modify the res.locals or app.locals object from the client side? My goal is conditionally render html based off of form selection options.
best bet is likely to use client side js for the dynamic html rendering, handlebars will render one time during initial request, that's it. IF you need backend data that's not available during first request/page load then use AJAX to fetch that data (via client side js script)
@@DevSproutthat was what I thought I needed to do, but I wasn't really sure if I could rely on modifying res.locals or app.locals without a GET or POST request. Thanks for the response, appreciate it!
i love you remember that!
amazing.
great
thanks
thanks