Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js passing parameters to client via express render

I'm using Node.js and I'm having issues communicating with a client.

I define Express:

var express             = require("express");
var app                 = express();`

When I try and pass a parameter to the client upon requesting a page the variable holds no data, for example:

app.get("/", function(req, res){
    res.render("index", { name: "example" });
});

On the index page, when I use the console to print the variable (name)it returns "".

More info: http://expressjs.com/api.html#app.render

Am I missing something or doing something wrong?

like image 853
g571792 Avatar asked Mar 29 '15 10:03

g571792


1 Answers

The variable name you sent to the render function is only available while rendering the page, after it is sent to the client, it is not accessible. You have to use it in your view on the rendering stage.

Since you are using handlebars, you can display it in your page like this, for instance:

<h1>{{ name }}</h1>

If you want to use this data in a javascript, use it inside a script tag:

<script>
  var name = "{{ name }}";
  console.log(name);
</script>
like image 179
victorkt Avatar answered Sep 24 '22 15:09

victorkt