Node.js passing parameters to client via express render

If you want to get parameters on the clientside via javascript, you should do template like this <script>var data = data</script>, otherwise variables aren't available

If you use Jade, it will be something like this:

script(type='text/javascript').
    var name = !{name}

You are basically telling express to render your index page and providing a value for the name variable, but that doesn't necessarily make the name var available in your client side javascript. You need to edit your index template to display the name variable in the page. The syntax varies depending on the templating engine you are using (jade, ejs, dustjs).

Another solution is to use an ajax call in your client page's javascript and use res.json on the server instead to send the data. Then you can evaluate name in the console. Ex using jquery:

index.html:

$.get( "/getvar", function( data ) {
  name = data.name;
});

server.js:

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

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>