[英]Node.js: Pass variables to a file
I am sending a . 我正在发送。 XML
file using res.sendfile 'xml/foo.xml'
. 使用res.sendfile 'xml/foo.xml'
XML
文件。 How can I pass variables to the foo.xml
file and change the .XML content accordingly? 如何将变量传递到foo.xml
文件并相应地更改.XML内容?
Thanks 谢谢
Instead of using res.sendfile 'xml/foo.xml'
, you'll have to use a template file and pass your variables into a library that will parse the template and give you the XML for each request. 您不必使用res.sendfile 'xml/foo.xml'
,而必须使用模板文件并将变量传递到一个库中,该库将解析该模板并为您提供每个请求的XML。 While there are many templating engines for Node.js, the two that Express supports by default are EJS and Jade . 尽管有许多用于Node.js的模板引擎,但Express默认支持的两个引擎是EJS和Jade 。 Jade is Haml-like, which is cool, but if you're working with existing XML, then you should probably stick to EJS. Jade类似于Haml,这很酷,但是如果您使用的是现有XML,则应该坚持使用EJS。 EJS lets you just take ordinary HTML or XML and embed JavaScript in it. EJS允许您仅采用普通的HTML或XML并将JavaScript嵌入其中。
So, you'd rename xml/foo.xml
to views/foo.ejs
and, instead of res.sendfile
, you'd write something like 因此,您将xml/foo.xml
重命名为views/foo.ejs
,而不是res.sendfile
,您将编写类似
res.contentType 'text/xml'
res.render 'foo.ejs', obj
where obj
contains all of the variables that you want to make available to the template. 其中obj
包含您要对模板可用的所有变量。
Check the Express guide on "View Rendering" for more information. 有关更多信息,请查看“查看渲染”中的Express指南 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.