node_express framework 02

node_express framework 01_You Xiaobei's Blog-CSDN Blog

09_routing modularization

When we use routing, we will inevitably encounter many visits. There are get requests for home, and there are also post requests  thought of. The first step is to separate the routing, and separate a class of routing into a separate file: we need to use the Router method in express, create an instance and then create routing rules, and finally export the router routing object. code show as below:

/**
 * 路由模块化,把路由规则放在独立的文件中,然后 require 导入使用
 */

const express = require("express"); // 导入express

// 创建路由对象,使用 express.Router() 方法
const router = express.Router();

// 创建路由规则
router.get("/router", (req, res) => {
  res.send("你访问了分离路由模块");
});

// module.exports 暴露出路由对象router
module.exports = router;

Then it is imported and used in the app.js file. Use the require method to import, and then use the app.use method to use:

const express = require("express");
const app = express();

/**
 * 导入路由模块,然后 app.use() 使用
 */

const router = require("./routerModule");

// 使用
app.use(router);

app.listen(8080, () => {
  console.log("服务开启在 8080 端口");
});

10_template engine EJS

Template engine is a technology that separates user interface (HTML) and business data (JS) . It is an efficient javascript template engine.

Official website: ejs , download: npm i ejs --save

10.1 Usage:

/**
 * 模版引擎是分离 用户界面(HTML) 和 业务数据(JS) 的一种技术
 * 他是一个高效的 javascript 模版引擎,https://ejs.bootcss.com/
 * 下载:npm i ejs --save
 *      类似之前我们使用的 ${} 模版字符串加强版
 */

// 1. 导入
const ejs = require("ejs");

// 2. 定义一个变量,后面使用
const str = "这是一个变量哦";

// 3. 使用 EJS 的渲染,介绍:
// <%= 某一个变量,需要在render 加入第二个对象参数 %>
let result = ejs.render("介绍:<%= str %>", { str: str });

console.log(result); //介绍:这是一个变量哦

Up to this point, we have not achieved the so-called separation of html and js. Next, we create a new html file and write the following content in it:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>EJS</title>
  </head>
  <body>
    <!-- 我们在里面写模版语法 -->
    <h2><%= str %></h2>
  </body>
</html>

Then import using:

const express = require("express");
const fs = require("fs");
const app = express();

// 1. 导入
const ejs = require("ejs");

// 2. 定义一个变量,后面使用
const str = "这是一个变量哦";

// 3. 读取html 文件 ~用 toString() 方法 buffer 转字符串
const html = fs.readFileSync("./index.html").toString();

// 4. 使用 EJS 的渲染
let result = ejs.render(html, { str: str });

// 5. 把 html 响应给客户端
app.get("/html", (req, res) => {
  res.send(result);
});

// 开启服务
app.listen(8080, () => {
  console.log("服务开启");
});

More template functions: For example, basic js code can also be written in the template, the code is as follows:

<body>
    渲染一个数组:
    <ul>
      <% arr.forEach((item)=> { %>
      <li><%= item %></li>
      <% }) %>
    </ul>
  </body>
...
// 2. 定义一个变量,后面使用
const arr = [1, 2, 3, 4, 5];

...

// 4. 使用 EJS 的渲染
let result = ejs.render(html, { arr: arr });


...

The result is as follows: 

10.2 Label meaning

  • <% 'script' tag, for flow control, no output.
  • <%_ remove the preceding whitespace
  • <%= Output data to template (output is escaped HTML tags)
  • <%- output unescaped data to the template
  • <%# Comment label, do not execute, do not output content
  • <%% output string '<%'
  • %> generic end tag
  • -%> remove the newline that follows
  • _%> Remove spaces after the closing tag

10.3 Custom delimiter 

10.3.1 For a single template file, use the delimiter parameter, the code is as follows:

let user = ['李','华'];

ejs.render('<?= users.join(" | "); ?>', {users: users},
    {delimiter: '?'});

10.3.2 Global use, add objects on ejs, the code is as follows:

ejs.delimiter = '$';

ejs.render('<$= users.join(" | "); $>', {users: users});

10.4 Layouts

EJS does not provide special support for blocks, but you can implement layout by including header and footer, the code is as follows:

<%- include('header'); -%>
<h1>
  Title
</h1>
<p>
  My page
</p>
<%- include('footer'); -%>

Guess you like

Origin blog.csdn.net/m0_66492535/article/details/129904157