如何从node.js服务器呈现变量而无需重新加载和不使用EJS/AJAX? [英] How to render variable from node.js server without have to reload and without using EJS/AJAX?

查看:100
本文介绍了如何从node.js服务器呈现变量而无需重新加载和不使用EJS/AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用ejs:

node.js:

var example = 0;
app.get("/", function(req, res, next) {
  res.sendFile(__dirname + "/public/index.ejs");
  res.render(__dirname + "/public/index.ejs", {example:example});
});

index.ejs:

index.ejs:

<p><%- example %></p>

但是如果变量已更改,我需要重新加载页面以更新变量.

But if the variable has been changed I needed to reload the page to update the variable.

推荐答案

您可以只使用轮询(网站会在x秒钟内重新加载):

You could just use polling (the website reloads all x seconds):

<head>
  <meta http-equiv="refresh" content="3">
</head>
<body>
 <p><%- example %></p>
</body>

这篇关于如何从node.js服务器呈现变量而无需重新加载和不使用EJS/AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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