Node.js 通过快速渲染将参数传递给客户端 [英] Node.js passing parameters to client via express render

查看:30
本文介绍了Node.js 通过快速渲染将参数传递给客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Node.js,但在与客户端通信时遇到问题.

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

我定义快递:

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" });
});

在索引页上,当我使用控制台打印变量 (name) 时,它返回 "".

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

更多信息:http://expressjs.com/api.html#app.render

我是否遗漏了什么或做错了什么?

Am I missing something or doing something wrong?

推荐答案

你发送给渲染函数的变量 name 只在渲染页面时有效,发送给客户端后,不可访问.您必须在渲染阶段的视图中使用它.

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>

如果您想在 javascript 中使用此数据,请在 script 标签中使用它:

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

<script>
  var name = "{{ name }}";
  console.log(name);
</script>

这篇关于Node.js 通过快速渲染将参数传递给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆