Node Express 中的 res.sendfile 与传递数据 [英] res.sendfile in Node Express with passing data along

查看:113
本文介绍了Node Express 中的 res.sendfile 与传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以从 Node.JS 应用程序重定向到 HTML 文件,例如:res.sendFile 表达并将JSON数据传递给html文件?

Is there any way to redirect to an HTML file from a Node.JS application with something like: res.sendFile of express and pass a JSON data along to the html file?

推荐答案

您从给定的请求中得到一个响应.您可以将多个事物组合成一个响应,也可以要求客户端发出单独的请求以获取单独的事物.

You get one response from a given request. You can either combine multiple things into one response or require the client to make separate requests to get separate things.

如果您想要做的是获取一个 HTML 文件并通过向其中插入一些 JSON 来修改它,那么您不能只使用 res.sendFile() 因为它只是读取一个来自磁盘或缓存的文件,并直接将其作为响应流式传输,不提供修改它的机会.

If what you're trying to do is to take an HTML file and modify it by inserting some JSON into it, then you can't use just res.sendFile() because that just reads a file from disk or cache and directly streams it as the response, offering no opportunity to modify it.

更常见的做法是使用模板系统,让您将内容插入到 HTML 文件中(通常用您自己的数据替换特殊标签).有数以百计的模板系统和许多支持 node.js.node.js 的常见选择是 Jade (Pug)、Handlebars、Ember、Dust、EJS、Mustache.

The more common way of doing this is to use a template system that lets you insert things into an HTML file (usually replacing special tags with your own data). There are literally hundreds of template systems and many that support node.js. Common choices for node.js are Jade (Pug), Handlebars, Ember, Dust, EJS, Mustache.

或者,如果您真的想这样做,您可以将 HTML 文件读入内存,在其上使用某种 .replace() 操作插入您自己的数据,然后 res.send() 更改后的文件.

Or, if you really wanted to do so, you could read the HTML file into memory, use some sort of .replace() operation on it to insert your own data and then res.send() the resulting changed file.

这篇关于Node Express 中的 res.sendfile 与传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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