We have established a views in the project folder (must be), if you do not want to use the views folder, then you need to call app.set ( "views", "custom folder name"), and then create a template file news.ejs inside :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<ul>
<% for(var i = 0;i<news.length;i++){%>
<li><%=news[i]%></li>
<%}%>
</ul>
</body>
</html>
Js file and then render binding relationship:
var express = require("express"); var app = express(); app.set("view engine","ejs"); app.get("/",function (req,res) { res.render("news",{ "news":["新闻1","新闻2","新闻3"] }) }).listen(3000);
Then start the service, access to port 3000, page output:
Reproduced in: https: //www.cnblogs.com/yk123/p/7190519.html