node. JS template Engine Learning----EJS

Source: Internet
Author: User

Environment: Windows+node.js+express

First, install the Ejs

Open the cmd window, enter NPM install EJS-G, and wait for the download to complete.

Second, use

The routing mechanism and template are used during the call, the routing request address is the root directory, and the template file is Home.ejs "Note: Template engine files are. ejs files"

1, new JS file for Index.js

Write the following code:

Varexpress=require ("Express"); varapp=express ();//Set Render engine App.set ("Viewengine", ' Ejs ');// Set the template directory to the current Index.js directory sibling views directory under the template App.set ("views", __dirname+ "/views");//settings using the current directory App.use (Express.static (__dirname ); App.get ("/", function (req,res) {//render page and transmit value res.render (' Home.ejs ', {name: "Kid"}); /Monitor 3000 port App.listen (3000);

2, the new home.js, the directory is/views/home.ejs, enter the following content:

<! Doctypehtml>

3. Enter localhost:3000 in the browser

The results are as follows

4, the introduction of Ejs in the Web page, rendering

Modify Home.js

<! Doctypehtml>

Summary: This can be extended to dynamically create or retrieve the locals parameter of the Res.render () method, and then render the same page, 4th step, as in the introduction of jquery to the Web page operation.

node. JS template Engine Learning----EJS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.