【问题标题】:While rendering html+js page in node.js using jade, getting html as text rendered instead of the html使用jade在node.js中渲染html + js页面时,将html作为文本渲染而不是html
【发布时间】:2014-07-07 20:17:31
【问题描述】:

我正在尝试使用 Jade 在 nodejs 中呈现 html+js 页面。我使用在线可用的 html 到翡翠转换器将我的 html 转换为翡翠,然后从我的 nodejs 应用程序中渲染它。但是,我无法正确渲染玉。 我使用的示例是 google 在他们的 google maps API 文档中提供的示例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Marker animations with google</title>
<style>
  html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
  #panel {
    position: absolute;
    top: 5px;
    left: 50%;
    margin-left: -180px;
    z-index: 5;
    background-color: #fff;
    padding: 5px;
    border: 1px solid #999;
  }
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
var berlin = new google.maps.LatLng(52.520816, 13.410186);

var neighborhoods = [
new google.maps.LatLng(52.511467, 13.447179),
new google.maps.LatLng(52.549061, 13.422975),
new google.maps.LatLng(52.497622, 13.396110),
new google.maps.LatLng(52.517683, 13.394393)
];

var markers = [];
var iterator = 0;

var map;
function initialize() {
var mapOptions = {
zoom: 12,
center: berlin
};

map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
}

function drop() {
for (var i = 0; i < neighborhoods.length; i++) {
setTimeout(function() {
  addMarker();
}, i * 200);
}
}

function addMarker() {
markers.push(new google.maps.Marker({
position: neighborhoods[iterator],
map: map,
draggable: false,
animation: google.maps.Animation.DROP
}));
iterator++;
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>
<body>
<div id="panel" style="margin-left: -52px">
  <button id="drop" onclick="drop()">Drop Markers</button>
 </div>
<div id="map-canvas"></div>
</body>
</html>

我尝试了许多转换器进行转换,但每个转换器都给了我一个或另一个错误。 http://html2jade.vida.io/ 能够把它变成玉。

我将文件作为 maps_marker.jade 保存在我的视图目录中,并从我的 web.js 中的一个 API 调用“res.render('maps_marker',{})”来呈现此页面。 但是页面将​​以下 html 呈现为文本

 <!DOCTYPE html><html><head><meta charset="utf-8"><title>Marker animations with google</title><style><html>, body, #map-canvas {height: 100%;margin: 0px;padding: 0px}</html><div id="panel">{position: absolute;top: 5px;left: 50%;margin-left: -180px;z-index: 5;background-color: #fff;padding: 5px;border: 1px solid #999;}</div></style><script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script><script><var>berlin = new google.maps.LatLng(52.520816, 13.410186);</var><var>neighborhoods = [</var><new>google.maps.LatLng(52.511467, 13.447179),</new><new>google.maps.LatLng(52.549061, 13.422975),</new><new>google.maps.LatLng(52.497622, 13.396110),</new><new>google.maps.LatLng(52.517683, 13.394393)</new>];<var>markers = [];</var><var>iterator = 0;</var><var>map;</var><function>initialize() {</function><var>mapOptions = {zoom: 12,center: berlin};</var><map>= new google.maps.Map(document.getElementById('map-canvas'),mapOptions);}</map><function>drop() {for (var i = 0; i < neighborhoods.length; i++) {setTimeout(function() {addMarker();}, i * 200);}}</function><function>addMarker() {markers.push(new google.maps.Marker({position: neighborhoods[iterator],map: map,draggable: false,animation: google.maps.Animation.DROP}));iterator++;}</function><google window load initialize class="maps event addDomListener"></google></script></head><body><div id="panel" style="margin-left: -52px"><button id="drop" onclick="drop()">Drop Markers</button></div><div id="map-canvas"></div></body></html>

谁能帮我理解如何在 node.js 中渲染这样的 html+js。 我目前正在尝试使用翡翠模板引擎,但也可以使用任何其他模板引擎。

已更新以从我的 web.js 添加 sn-p 并输入来自

我有一个 index.html(在公用文件夹中),其中包含以下表单

<form id="searchForm" action="/submit_search" method="POST" enctype="multipart/form-data">
    <input name="latitude" type="text" id="latitude"/>
    <input name="longitude" type="text" id="longitude"/>
    <input type="submit" value="Submit" onclick="submitForm()">
</form>

在我的 javascript - 我提交表单。 在我的 web.js 中,我有以下用于 submit_search API 的 sn-p

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.post('/submit_search', function(req,res){
    console.log(JSON.stringify(req.body));
    pg.connect(database_url, function(err, pgclient, done){
            if(err)
            {
                    console.log("Error in fetching pgclient from pool");
                    res.send(500, "Load the error connection page");
            }
            else if(pgclient != null)
            {
                    console.log("Got an instance of pgclient");
                    generateDataForMap(req, res, pgclient, done);
            }
    });
});

然后我定义了我的 generateDataForMap 方法

var generateDataForMap = function(req, res, pgclient, done){
  ... some processing.....
  ..... create the required json.....
    res.render('maps_marker',json);
}

我已将 maps_marker.jade 文件放在 views 文件夹中..

【问题讨论】:

    标签: node.js google-maps pug template-engine


    【解决方案1】:

    http://html2jade.vida.io/生成的jade文件无效。要修复它,您需要在stylescript 标签之后添加一个.(点字符),以分别将它们转换为style.script.

    doctype html
    html
      head
        meta(charset="utf-8")
        title Marker animations with google
        style.
          html, body, #map-canvas {
          height: 100%;
          margin: 0px;
          padding: 0px
          }
          #panel {
          position: absolute;
          top: 5px;
          left: 50%;
          margin-left: -180px;
          z-index: 5;
          background-color: #fff;
          padding: 5px;
          border: 1px solid #999;
          }
        script(src="https://maps.googleapis.com/maps/api/js?v=3.exp")
        script.
          var berlin = new google.maps.LatLng(52.520816, 13.410186);
          var neighborhoods = [
          new google.maps.LatLng(52.511467, 13.447179),
          new google.maps.LatLng(52.549061, 13.422975),
          new google.maps.LatLng(52.497622, 13.396110),
          new google.maps.LatLng(52.517683, 13.394393)
          ];
          var markers = [];
          var iterator = 0;
          var map;
          function initialize() {
          var mapOptions = {
          zoom: 12,
          center: berlin
          };
          map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);
          }
          function drop() {
          for (var i = 0; i < neighborhoods.length; i++) {
          setTimeout(function() {
          addMarker();
          }, i * 200);
          }
          }
          function addMarker() {
          markers.push(new google.maps.Marker({
          position: neighborhoods[iterator],
          map: map,
          draggable: false,
          animation: google.maps.Animation.DROP
          }));
          iterator++;
          }
          google.maps.event.addDomListener(window, 'load', initialize);
      body
        #panel(style="margin-left: -52px")
          button#drop(onclick="drop()") Drop Markers
        #map-canvas
    

    【讨论】:

    • 感谢您的帮助。但是当我尝试加载 html 时,html 仍然作为文本加载,而不是 html 被加载。前面说了,我把jade文件命名为maps_marker.jade,放在views下。在我的 API 中,我将其称为 res.render(maps_marker);
    • 能否用您的 web.js 服务器的相关摘录更新您的问题?
    • 感谢您的跟进。我已经用更多数据更新了这个问题。
    • 我能够加载页面。谢谢你帮我整了玉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多