Home Web Front-end HTML Tutorial Express code analysis using html template

Express code analysis using html template

Jun 11, 2018 pm 05:20 PM
express html template

This article mainly introduces the detailed code of Express using html template. The content is quite good. Now I share it with you and give it as a reference. Let’s follow the editor and take a look.

Express uses the jade template by default and can be configured to support the use of ejs or html templates.

1. Install ejs

Install ejs in the project root directory.

npm install ejs
Copy after login

2.Introduce ejs

var ejs = require('ejs'); //我是新引入的ejs插件
Copy after login

3. Set the html engine

app.engine('html', ejs.__express);
Copy after login

Set the view engine

app.set('view engine', 'html');
Copy after login

After saving, restart the service to access the html file.

Note: In the server built by express, the html engine is not configured, just add it directly; the view engine is configured, just modify the configuration.

-------------------------------------------------- ----------------------------------------

What did you do with these modified settings?

Why do I need to add settings to the html engine after modifying the view engine?

Let’s take a look at the .engine() method first.

app.engine(ext, callback);
Copy after login

Express uses the jade template by default. If you try to load the "foo.jade" file, Express will internally call the following operations.

app.engine('jade', require('jade').__express);
Copy after login

If you want to use other template engines, such as mapping EJS templates to ".html" files:

app.engine('html', require('ejs').__express);
Copy after login

In this line of code, the .renderFile() method of EJS is actually called. ejs.__express is another name for this method inside EJS.

Because the loaded template engine calls the same method .__express, so if you are using an ejs template, you do not need to configure this item.

Summary: To use html template, you need to add app.engine('html', require('ejs').__express);

When using EJS template, there is no need to configure this item.

At this time, if you create an index.html file or an index.ejs file in the views folder, the default index.jade file will still be accessed. Why is this? What I want to talk about here is the second setting mentioned above app.set('view engine', 'html');

app.set(name, value);

Among the parameters of the .set() method, one item is 'view engine', which indicates the engine plug-in used by default when the file template format is not specified. If this is set to an html file, when setting the route to specify the file, you only need to write the file name and the corresponding html file will be found. At this point, my imagination opened up and I tried to create three files test.jade, test.ejs, and test.html in views. The routing settings are as follows. Access is normal! Each route points to the corresponding file. Of course, this way of writing is not recommended at all and is not consistent with reality.

router.get('/test/',function(req, res, next){
 res.render('test', {title: 'HTML'});
});

router.get('/test1/',function(req, res, next){
 res.render('test.ejs', {title: 'EJS'});
});

router.get('/test2/',function(req, res, next){
 res.render('test.jade', {title: 'jade});
});
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Using Requirejs in Html for modular development analysis

The above is the detailed content of Express code analysis using html template. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

In-depth comparison of Express and Laravel: How to choose the best framework? In-depth comparison of Express and Laravel: How to choose the best framework? Mar 09, 2024 pm 01:33 PM

In-depth comparison of Express and Laravel: How to choose the best framework? When choosing a back-end framework suitable for your project, Express and Laravel are undoubtedly two popular choices among developers. Express is a lightweight framework based on Node.js, while Laravel is a popular framework based on PHP. This article will provide an in-depth comparison of the advantages and disadvantages of these two frameworks and provide specific code examples to help developers choose the framework that best suits their needs. Performance and scalabilityExpr

Comparative analysis of Express and Laravel: Choose the framework that suits you better Comparative analysis of Express and Laravel: Choose the framework that suits you better Mar 10, 2024 pm 10:15 PM

Express and Laravel are two very popular web frameworks, representing the excellent frameworks of the two major development languages ​​of JavaScript and PHP respectively. This article will conduct a comparative analysis of these two frameworks to help developers choose a framework that is more suitable for their project needs. 1. Framework Introduction Express is a web application framework based on the Node.js platform. It provides a series of powerful functions and tools that enable developers to quickly build high-performance web applications. Express

Let's talk about how node+express operates cookies Let's talk about how node+express operates cookies Jun 22, 2022 am 10:01 AM

How does node+express operate cookies? The following article will introduce to you how to use node to operate cookies. I hope it will be helpful to you!

Express vs. Laravel: Comparing the advantages and disadvantages, which one will you choose? Express vs. Laravel: Comparing the advantages and disadvantages, which one will you choose? Mar 10, 2024 am 08:39 AM

Express vs. Laravel: Comparing the advantages and disadvantages, which one will you choose? In the field of web development, Express and Laravel are two frameworks that have attracted much attention. Express is a flexible and lightweight web application framework based on Node.js, while Laravel is an elegant and feature-rich web development framework based on PHP. This article will compare the advantages and disadvantages of Express and Laravel in terms of functionality, ease of use, scalability, and community support, and combine

How to build a full-stack JavaScript application using React and Express How to build a full-stack JavaScript application using React and Express Sep 26, 2023 pm 01:09 PM

How to use React and Express to build a full-stack JavaScript application Introduction: React and Express are currently very popular JavaScript frameworks. They are used to build front-end and back-end applications respectively. This article will introduce how to use React and Express to build a full-stack JavaScript application. We will explain step by step how to build a simple TodoList application and provide specific code examples. 1. Preparation before starting

How does PHP handle HTML templates and page layout? How does PHP handle HTML templates and page layout? Jul 01, 2023 am 10:46 AM

PHP, as a popular server-side scripting language, is widely used to develop web pages and websites. In web development, HTML templates and page layouts are important components. This article will explore how PHP handles HTML templates and page layout. First of all, HTML template is the skeleton of a web page, including the structure, layout and static content of the web page. There are many ways to process HTML templates in PHP. Two commonly used methods are introduced below. The first method is to use the native syntax of PHP itself and embed the HTML code directly into

Express or Laravel? Choose the backend framework that works best for you Express or Laravel? Choose the backend framework that works best for you Mar 10, 2024 pm 06:06 PM

When it comes to choosing a backend framework, both Express and Laravel are very popular choices. Express is a web application development framework based on Node.js, while Laravel is a web application development framework based on PHP. Both have their own advantages, and choosing the framework that best suits you requires considering many factors. The strengths of the Express framework are its flexibility and easy learning curve. The core idea of ​​Express is "small enough and flexible enough", and it provides a large number of middleware

See all articles