zoukankan      html  css  js  c++  java
  • nodejs 模板引擎ejs的使用

    1、test.ejs文件

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <% for(var i=0;i<json.arr.length;i++){ %>
        <div>用户名:<%=json.arr[i].user%> 密码:<%=json.arr[i].pass%></div>
        <% } %>
    </body>
    </html>

    2、ejs.js文件

    var ejs=require('ejs');
    var http = require('http');
    ejs.renderFile('cleaning.ejs', {json: {arr: [
                {user: '马武', pass: '1269'},
                {user: '张三', pass: '7654'},
                {user: '王二', pass: '2143'}
            ]}}, function (err, data){
        //创建服务
        http.createServer(function(req,res){
            res.writeHead(200,{'Content-Type':'text/html'});
            res.write(data);
            res.end('<p>渲染结束</p>');
        }).listen(8080,"localhost",function () {
            console.log("open server at port:8080...");
        });
    });

    3、浏览器直接访问接口8080即可展示页面

  • 相关阅读:
    oracle-PL/SQL1
    ROS之Gazebo
    ROS之urdf 2
    ROS之urdf 1
    ROS 面部识别
    ROS x Arduino
    STM32F0的低功耗模式
    项目进度
    C++函数返回为引用
    STM32F0的多路ADC 无DMA
  • 原文地址:https://www.cnblogs.com/gopark/p/9779595.html
Copyright © 2011-2022 走看看