Send data to the forefront in Express and access to it

I'm new to expression and pretty confused about how I should access data on the interface side,

I have the following code

exports.init = function(req, res){ if (req.isAuthenticated()) { //res.send(); var userId = req.user._id res.render('account/usernotes'); } else { res.render('signup/index', { oauthMessage: '', oauthTwitter: !!req.app.config.oauth.twitter.key, oauthFacebook: !!req.app.config.oauth.facebook.key, oauthGoogle: !!req.app.config.oauth.google.key }); } }; 

I want to send UserId to the view so that I can access it so that I can receive data using it, but still use send (); displays the data value in the interface, so how do I access it. I guess

 res.render('account/usernotes',req.user._id); 

should work, but how do I access the data after that?

+5
source share
2 answers

When I use Jade / EJS templates, it is something like this:

 res.render('account/usernotes', {userid: req.user._id}); 

In the jade file:

 p #{userid} 

In the ejs file you can:

 <%=userid%> 
+7
source

Here is a way to send data from server to client.

Here is your server side code.

 res.render('index', { data1: 'Hello', data2: 'World' }); 

Get the data on your view, which is index.ejs.

 <h1> <%= data1 + ' ' + data2 %> </h1> 

See this for more details.

+2
source

Source: https://habr.com/ru/post/1202838/


All Articles