nodejs-在node中使用模板引擎art-template渲染页面

web浏览器里的art-template是这样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<body>
<!-- 引入模板 -->
<script src="node_modules/lib/art-template"></script>
<script type="text/template" id="tpl">
{{ name }}
{{ each hobbies }}
{{ $value }}
{{ /each }}
</script>
<script>
var ret = template('tpl', {
name : 'zhangsan'
hobbies : [
'football',
'soccer'
]
})
</script>
</body>

node中的art-template

  • 安装

    1
    npm install art-template
  • 加载模块

    1
    var template = require('art-template')
  • 使用模板引擎

    1
    2
    3
    var ret = template.render(模板字符串, {
    //替代的内容
    })

客户端和服务端渲染的区别

  • 客户端渲染:(异步渲染)
    • 两次请求
      • 页面请求
      • Ajax数据请求
    • 不利于SEO搜索引擎优化
      • 很难被爬虫抓取到
    • 右键查看网页源代码查不到
      • 比如:商品评论
  • 服务端渲染:
    • 一次请求
    • 有利于SEO搜索引擎优化
      • 很难被爬虫抓取到
    • 右键查看网页源代码可查到
      • 比如:商品信息

        所以,真正的网站都是两种渲染结合来做的
        例如:京东
        商品列表采用服务端渲染,利于搜索引擎抓取
        商品评论为了用户体验,而且不需要SEO优化

# nodejs

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×

// tidio机器人助手