Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Express和node.js中传递变量_Javascript_Node.js_Express - Fatal编程技术网

Javascript 如何在Express和node.js中传递变量

Javascript 如何在Express和node.js中传递变量,javascript,node.js,express,Javascript,Node.js,Express,我正在尝试使用node.js和express做一些事情。 但我在将变量快速传递到.js时遇到了一些问题 这是我的代码,我想知道.js中的IP地址,所以我做了以下事情 像这样,我将用户重定向到test.js app.get('/', function(req, res) { app.set('ipAddr' , req.ip); res.sendfile(__dirname + '/index.html'); }); 和index.html包括 <script src="/hel

我正在尝试使用node.js和express做一些事情。 但我在将变量快速传递到.js时遇到了一些问题

这是我的代码,我想知道.js中的IP地址,所以我做了以下事情

像这样,我将用户重定向到test.js

app.get('/', function(req, res) {
  app.set('ipAddr' , req.ip);
  res.sendfile(__dirname + '/index.html');
});
和index.html包括

 <script src="/hello.js"></script>

因此,在hello.js中,我想知道客户端IP地址

我想知道怎么做,但失败了


有人能告诉我怎么做吗?非常感谢。

客户端的
IP
可以在
req.connection.remoteAddress
中找到。您可以使用
res.render
将变量传递到页面,或者使用
res.send

谢谢,但是如果我使用res.render,它无法重定向到index.html,如果我使用res.render,你能举例说明如何从客户那里得到变量吗?