Express code analysis using 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
2.Introduce ejs
var ejs = require('ejs'); //我是新引入的ejs插件
3. Set the html engine
app.engine('html', ejs.__express);
Set the view engine
app.set('view engine', 'html');
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);
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);
If you want to use other template engines, such as mapping EJS templates to ".html" files:
app.engine('html', require('ejs').__express);
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}); });
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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? 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

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

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? 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 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

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

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
