I have a static HTML page, and I am using NodeJS to serve that page.
我有一个静态的HTML页面,我使用NodeJS来服务这个页面。
I want to be able to give the HTML page URLS of images dynamically from NodeJS so that those images can be displayed.
我希望能够从NodeJS动态地提供HTML页面url,以便显示这些图像。
How would I do this?
我该怎么做呢?
1 个解决方案
#1
2
Use a templating engine of some sort. An easy view template engine to start with is called Handlebars. This will allow you to dynamically create "layouts" for the bulk of the html page that stays the same, while at the same time allow you to create "views" which are the partial html files that will be inserted to the layout. An example of this would be a header and a footer. The header and footer will most likely stay the same throughout your website so you would put that in your layout file and inside of the body is where the dynamic views can be rendered.
使用某种模板引擎。一个简单的视图模板引擎被称为Handlebars。这将允许您动态地为大部分保持不变的html页面创建“布局”,同时允许您创建“视图”,这些“视图”是将插入到布局中的部分html文件。一个例子就是页眉和页脚。页眉和页脚很可能在整个网站中保持一致,所以你可以把它放在你的布局文件中,在主体内部,动态视图可以被呈现。
Handlebars uses this notation to render httpRequest data inside of your html file:
Handlebars使用这个符号来呈现html文件中的httpRequest数据:
html
html
<div id='user-profile-image'> {{profile_image_uri}} </div>
js
js
app.get('/user-by-name', function(req, res) {
var options =
{
"Content-Type" : "application/json",
"url" : "blah.com"
}
// Using the request module
request.get(options, function(err, httpResponse, body) {
if (err)
{
console.log(err)
return
}
// First param is your view name
// Second param is your json response body which is rendered in
// the view using handlebars {{}}
res.render('your_view_name_here', JSON.parse(body))
}
}
#1
2
Use a templating engine of some sort. An easy view template engine to start with is called Handlebars. This will allow you to dynamically create "layouts" for the bulk of the html page that stays the same, while at the same time allow you to create "views" which are the partial html files that will be inserted to the layout. An example of this would be a header and a footer. The header and footer will most likely stay the same throughout your website so you would put that in your layout file and inside of the body is where the dynamic views can be rendered.
使用某种模板引擎。一个简单的视图模板引擎被称为Handlebars。这将允许您动态地为大部分保持不变的html页面创建“布局”,同时允许您创建“视图”,这些“视图”是将插入到布局中的部分html文件。一个例子就是页眉和页脚。页眉和页脚很可能在整个网站中保持一致,所以你可以把它放在你的布局文件中,在主体内部,动态视图可以被呈现。
Handlebars uses this notation to render httpRequest data inside of your html file:
Handlebars使用这个符号来呈现html文件中的httpRequest数据:
html
html
<div id='user-profile-image'> {{profile_image_uri}} </div>
js
js
app.get('/user-by-name', function(req, res) {
var options =
{
"Content-Type" : "application/json",
"url" : "blah.com"
}
// Using the request module
request.get(options, function(err, httpResponse, body) {
if (err)
{
console.log(err)
return
}
// First param is your view name
// Second param is your json response body which is rendered in
// the view using handlebars {{}}
res.render('your_view_name_here', JSON.parse(body))
}
}