我怎样才能转发传递给nodejs的查询string通过res.render服务的页面?

在我的nodejs文件中,我正在渲染一个名为foo.html的页面。 在foo.html里面,我使用ajax从查询string中提取variables,并相应地加载一个合适的xml文档。 问题是,如果我运行我的nodejs服务器,我们将它称为l​​ocalhost,并追加查询string,然后服务器将消耗查询string,而不是foo.html中的ajax调用。

有没有办法将查询string直接转发到foo.html?

从nodejs文件中,server.js:

app.get('/', function (req, res) { var query = req.query; if (query) { if (query.screen == "page1") { res.render('foo.html'); } } }); 

我可以想到2种方法

 1st way -) you can change your rendering engine and pass the query string variables as local variables to the template and store those values as variables inside a <script></script> tag 2nd way -) keep the html but don't render the file load it and modify it to contain the same script tag with the variables, 

在这两种情况下,一旦你的页面加载你的JS将能够访问variables

————- HTML文件—————-

 <script> var a=/*text-to-replace-for-A*/; var b=/*text-to-replace-for-B*/; </script> 

————–请求处理程序———–

  var fs=require('fs'); //this goes in the require section //this goes inside your function fs.readFile('/etc/passwd', function (err, data) { if (err) throw err; var finalHTML=data.replace(/\/\*text-to-replace-for-A\*\//g,variableA).replace(/\/\*text-to-replace-for-B\*\//g,variableB); res.send(finalHTML) }); 

像这样的事情应该注意到我正在使用一个简单的读取文件,并发送不是最快的解决scheme,但是最简单的使用html修改。 您也可以使用stream来即时修改内存中的文件。 您可以在http://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback中findreadfile函数参&#x8003;