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

问题描述

有没有办法从Node.JS应用程序重定向到HTML文件,例如:,并将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,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, 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.send文件传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:26