简体   繁体   English

在node.js中请求带有把手内容的html文件

[英]Requesting an html file with handlebars content in node.js

First question so I hope my formatting works. 第一个问题,所以我希望我的格式化工作正常。 I've got a node server running that works great. 我有一个运行良好的节点服务器。 I'm writing a simple javascript file to get HTML files from this server that looks like this 我正在编写一个简单的javascript文件以从该服务器获取HTML文件,如下所示

#!/usr/bin/env node
var request = require('request');

request.get("http://my.ip.address/htmlTemplate.html", function (error, response, body){
    if(!error){
        console.log(body);
        var x = body;
    }
});

and the HTML templates have Handlebars included in them, like so: HTML模板中包含了Handlebars,如下所示:

<!-- htmlTemplate.html -->

<div class="fake" id="thing1"></div>

{{#if foo}}
    <div class="conditional_html">
        All this stuff inside here.
    </div>
{{/if}}

<div class="{{bar}} class2" id="thing2"></div>

The request works and I receive the content, but the content I get back from the response is this: 该请求有效并且我收到了内容,但是我从响应中得到的内容是这样的:

<div class="fake" id="thing1"></div>


<div class=" class2" id="thing2"></div>

The handlebars content is omitted. 车把内容被省略。 I'd like to get the file in it's raw, uncompiled form (as plain text). 我想以原始的,未编译的形式(作为纯文本)获取文件。 Is this possible with an http request? http请求可以吗? Can I read this file remotely in way similar to fs ? 我可以用类似于fs方式远程读取此文件吗?

The Handlebars content isn't omitted. 把手的内容不会省略。 You haven't provided the value of foo in your request, so if foo returns false . 您尚未在请求中提供foo的值,因此, if foo返回false Modifying the request url to something like http://my.ip.address/htmlTemplate.html?foo=true should make the conditional content get rendered in the final response. 将请求url修改为http://my.ip.address/htmlTemplate.html?foo=true可以使条件内容在最终响应中呈现。

As far as avoiding the rendering is concerned, it depends upon the configuration of the server which is processing the request ( http://my.ip.address in your case). 就避免呈现而言,它取决于正在处理请求的服务器的配置(在您的情况下为http://my.ip.address )。 If all documents responses are behind a rendering engine (which is normally the case), you can't bypass the rendering. 如果所有文档响应都在渲染引擎后面(通常是这种情况),则无法绕过渲染。 You can, however, selectively (manually) set certain routes to serve responses without compiling. 但是,您可以有选择地(手动)设置某些路由以提供响应而无需编译。 Simply send the response without calling Handlebars.compile() or template() . 只需发送响应而无需调用Handlebars.compile()template()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM