preface

We’ve all used the JS template engine in the browser before, but it can also be used in Node.js. The concept of using a template engine was first introduced on the server side.

art-template

Art-template is a high-performance JavaScript template engine that can be used not only in the browser (front end) but also in Node.

GitHub address: art-template

Document: Official Chinese

In this section we will explore the use of the Art-template template engine in Node to generate a standard HTML document.

The main body

  1. The principle of art-template is to take a string of data and replace it with certain formatting data according to the syntax of the template engine, and then produce the HTML document we want. Art-template supports two syntax types: standard syntax and primitive syntax. Standard syntax is usually {{}}, and primitive syntax is usually <% %>. For those who are not familiar with the art-template syntax, please refer to the official documentation.

  2. Install the art-template third-party package

Install art-template in the current project directory NPM, which will be installed in the current project node_modules folder by default.

  1. Created in the projecttemplate.htmlFile, which is used here to write our template HTML document. The specific code is as follows:
<! DOCTYPE html> <html lang="zh">
<head>
  <meta charset="UTF-8"> < title > {{title}} < / title > < / head > < body > < h1 > {{the main}} < / h1 > < p > my name is {{name}}, {{age}} this year, I my hobbies are: {{each hobbies}} {{$value}}, {{/each}}</p> </body> </ HTML >Copy the code

This section uses the standard syntax of art-template. Ensure that the correct template syntax is used when writing, otherwise the result will be incorrect.

  1. Create in the projectserver.js, to create a server, and importart-templateThe package.
let http = require('http')
let fs = require('fs')
let template = require('art-template')

let server = http.createServer()

server.on('request', (req, res) => {
  let url = req.url
  if (url === '/') {// Read the contents of template.html and convert it to a string format fs.readfile ('./template.html', (error, data) => {
      if (error) {
        return res.end('can not find template.html')}let dataStr = data.toString()
      let htmlStr = template.render(dataStr, {
        name: 'pubdreamcc',
        Title: 'home',
        main: 'Personal Information',
        age: 24,
        hobbies: ['Write code'.'read'.'basketball'}) // the template.render() method compiles the template document and returns the render result. res.end(htmlStr) }) }else {
    res.end('404 NOT found'Server.listen (3000, () => {console.log() => {console.log()'The server is started and can access... ')})Copy the code
  1. After the server is started and accessed by the browser, the result is as follows:

Here we have achieved our initial effect, should be relatively simple, ha ha. If you like it, I hope you can give me a star. Your thumbs up are my motivation to keep updating.

instructions

This warehouse is the real record of my node.js learning process, and I will update some new knowledge points every day in the future, hoping to be of some help to the students who want to learn Node.js. Welcome star, your praise is my lasting power to update. At the same time, if you think there are mistakes in some knowledge points in this warehouse, you can also issue me, so that I can make corrections later!

The warehouse at the same time in the blog garden and nuggets update, welcome to write blog friends to learn and exchange.

Blog garden

Looking for my

The Denver nuggets

Looking for my

GitHub

Looking for my