Passing variables from server to a page

6,440 views
Skip to first unread message

Masiar

unread,
May 21, 2011, 5:50:14 AM5/21/11
to expre...@googlegroups.com
Hi all,
Would it be possible to pass parameters I have in Express to a page I want to load?
For example:

app.get('/myParameterPage', function(req, res){
[...]
//somehow computed:
var foo = 'bar';
[...]

if(req.session.userAuthenticated){
res.sendfile(__dirname + '/mypage.html');
}
});

To put it simple I would like to pass the variable foo to mypage.html. Is it possible?

Thanks,
Masiar

Josh Chaney

unread,
May 21, 2011, 6:47:12 AM5/21/11
to expre...@googlegroups.com
Yep.. just like this:

app.get('/', function(req, res){
    var foo = 'bar';
    res.render('index.jade', { myvar: foo });
});

-- 
Josh Chaney

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

Masiar

unread,
May 22, 2011, 12:43:05 PM5/22/11
to Express
Thanks a lot. Would it be possible to do without templates like .jade?
I've tried to struggle a little bit with it, but for the purpose I
need to achieve it's still a lot of effort, I would rather prefer a
similar approach with static pages (mypage.html for example). In this
case would it be possible to do it? Or this parameter passing thing is
just available with templates?
Thanks,

Masiar

vision media [ Tj Holowaychuk ]

unread,
May 22, 2011, 3:46:01 PM5/22/11
to expre...@googlegroups.com
if you want something closer to html you may want to use ejs or mustache
--
Tj Holowaychuk
Vision Media
President & Creative Lead

Masiar

unread,
May 22, 2011, 4:13:52 PM5/22/11
to Express
Ok, thanks for pointing out those resources.
Does your answer implicitly says that it's impossible to do that with
plain html?

Masiar

On May 22, 9:46 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-

TJ Holowaychuk

unread,
May 22, 2011, 4:15:31 PM5/22/11
to expre...@googlegroups.com
well you can't really have dynamic output without some kind of template engine, passing vars to html wont do anything

-- 
TJ Holowaychuk

Masiar

unread,
May 22, 2011, 4:17:32 PM5/22/11
to expre...@googlegroups.com
Well, as far as I know (I may be wrong though), it's possible in JavaScript to parse the URL, so maybe there was some sort of parameter passing via URL, sort of PHP-like structure... but ok, then sorry for the dumb question. I'll go with either of the two template engine you pointed out.

Masiar

Masiar

unread,
May 22, 2011, 4:39:56 PM5/22/11
to Express
Ok, thanks for pointing out those resources.
Does your answer implicitly says that it's impossible to do that with
plain html?

Masiar

On May 22, 9:46 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-
media.ca> wrote:

TJ Holowaychuk

unread,
May 22, 2011, 4:42:52 PM5/22/11
to expre...@googlegroups.com
php is very different from most, php is essentially a template engine of it's own hence the <?php tags, basically you have to ignore everything you learnt with php haha, then things will make sense

-- 
TJ Holowaychuk

Masiar

unread,
May 24, 2011, 5:25:38 AM5/24/11
to Express
Ahah ok I will remember that :).
By the way, on the Express website it's pointed out that when I try to
render some .ejs file, the system will require automatically the ejs
engine, but when I try to render my .ejs I get this error:


Error: Cannot find module 'ejs'
at Function._resolveFilename (module.js:320:11)
at Function._load (module.js:266:25)
at require (module.js:364:19)
at View.templateEngine (/Users/Masiar/Desktop/USI/Master Second
Semester/Software Architecture/JSChess/server/node_modules/express/lib/
view/view.js:130:38)

etc.
Does this mean I should import myself the ejs module in my
application? Or should I just download the ejs module and put it in
some of the Express folders?
Thanks

Masiar

Laurie Harper

unread,
May 24, 2011, 7:21:15 AM5/24/11
to expre...@googlegroups.com
Since Express allows a wide variety of different template engines, it wouldn't be practical to install them all by default. You need to install whichever one you choose to use yourself. For ejs, simply to 'npm install ejs' from the root of your project.

L.

--
Laurie Harper
http://laurie.holoweb.net/

Reply all
Reply to author
Forward
0 new messages