如何在JavaScript中呈现HTML文件? [英] How do I render an html file in javascript?

查看:92
本文介绍了如何在JavaScript中呈现HTML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在使用javascript服务器端,包括node.js.由于性能问题,我们决定将一页移至服务器端而不是客户端,因此服务器将完全渲染的html流返回给客户端.

OK, I am using javascript sever side, including node.js. Because of performance issues, we have decided to move one page to being rendered server-side, not client side, so the server returns a stream of html, fully rendered, back to the client.

我已经看到这个问题和相关答案,但想知道这是最佳还是正确的方法.特别是,呈现页面并在js或node.js调用中在页面上运行所有javascript的最合适方法是什么?

I have seen this question and the related answers, but wondered if this was the best or right approach. In particular, what is the most appropriate way to render a page, and run all of the javascript on it within a js or node.js call?

我看过的想法:

  1. 直接在页面上调用javascript代码,并将所有内容反转以使其生成所需的html项.由于这很紧急,所以我宁愿避免不必要的重写.

  1. Call the javascript code directly on the page, and invert everything to make it generate the html items needed. As this is urgent, I would rather avoid re-writing any more than I have to.

使用简单的iframe呈现文档以生成html.但是,由于我是服务器端,如何指向iframe中的页面?当然,这只是为同一问题添加了另一个抽象层次.

Render a document, with a simple iframe to generate the html. But how do I point to the page in the iframe, as I am server side? Surely this is just adding another level of abstraction to the same problem.

使用上面详述的思想,但是考虑到我所遇到的一些问题,我想知道这是否是正确的途径.

Using the ideas detailed above, but I am wondering whether this is the right route, given some of the problems I have seen encountered with it.

只是为了澄清-实际上,我想将html页面加载到浏览器中,让它完成渲染,然后捕获整个生成的html以传递给客户端(节省了在html上渲染的时间).客户).

Just to clarify - I want to, in effect, load the html page in a browser, let it finish rendering, and then capture the entire generated html for passing through to the client (saving the time to render on the client).

推荐答案

这是一个简单的示例,用于服务器端模板(无表达):

This is a simple example that does server-side templating (no express): https://github.com/FissionCat/handlebars-node-server-example

这是提供html,js,css和mp3(但不使用express或任何模板)的示例: https://github.com/FissionCat/Hue-Disco

This is an example that serves html, js, css and an mp3 (but doesn't use express or any templating): https://github.com/FissionCat/Hue-Disco

这篇关于如何在JavaScript中呈现HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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