我創建了一個簡單的服務器(localhost:8080)來使用小鬍子打印文件夾中的相冊名稱,但它不起作用。
小鬍子不在Nodejs服務器上呈現對象
的方式我的設置功能是
1.我運行終端
2.本地主機的server.js(寫的NodeJS):8080 /頁/家在server.js解析瀏覽器結果該請求,並在basic.html調用home.js
我home.js
3. basic.html電話是:
$(function()
{
var tmpl, // main template - html
tdata= {}; // json data object feeding the template
// Initialise page
function show()
{
// Load html template
$.get("/templates/home.html", function(d)
{
tmpl= d;
});
//Retrieve server data, then initialize the page
$.getJSON("/albums.json", function(d)
{
$.extend(tdata, d.data);
});
// After completion of AJAX calls, parse the template
// replacing mustache tags with vars
$(document).ajaxStop(function()
{
var finald= mine(tdata);
var rendp= Mustache.render(tmpl, finald);
$("body").text(rendp);
});
}
show();
});
function mine(data)
{
if(data.albums && data.albums.length> 0)
data.have_albums= true;
else
data.have_albums= false;
return data;
}
以上(home.js )調用home.html的,那就是:
<div id="alblis">
{{#have_albums}}
<p> Number of albums: {{albums.length}}
</p>
{{/have_albums}}
<ul id="albums">
{{#albums}}
<li class="album">
<a href=
"http://localhost:8080/albums/{{album_name}}"> {{album_name}}
</a>
</li>
{{/albums}}
{{^albums}}
<li> Sorry, no albums </li>
{{/albums}}
</ul>
</div>
因此,有明顯的Mustache.render(..,..)home.js的一部分
問題之後按Enter在瀏覽器中http://localhost:8080/pages/home
,與server.js跑,我得在終端運行server.js
incoming req.: (GET) /pages/home
incoming req.: (GET) /content/jquery.js
incoming req.: (GET) /content/mustache.js
incoming req.: (GET) /content/home.js
incoming req.: (GET) /templates/home.html
incoming req.: (GET) /albums.json
邊注意以下事項:在終端上運行curl localhost:8080/albums.json
的結果是
{"error":null,"data":{"albums":[{"album_name":"friends","title":"friends"},{"album_name":"travel","title":"travel"}]}}
我想看看在瀏覽器中專輯名稱上運行:http://localhost:8080/pages/home
但情況並非如此!