var express = require("express");
var app = express();
app.use(express.static("./public"));
app.listen(3000);
这样public就是可以访问的静态文件了
例:
打开的页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>哈哈哈</h1>
<ul>
<% for(var i =0; i < news.length; i++){ %>
<li><%= news[i] %></li>
<% } %>
</ul>
</body>
</html>
js文件显示调用
var express = require("express");
var app = express();
//引入ejs
app.set("view engine", "ejs");
app.get("/", function (req, res) {
res.render("haha", {
"news" : ["①", "二", "三"]
});
});
app.listen(3000);