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