header.png
node+express+ejs+bootstrap是前端常用的項目結構
您可以通過git clone https://gitee.com/zhangyubk/MyProject.git
來克隆我創(chuàng)建好的項目結構,也可以通過下面的方式一步一步手動創(chuàng)建項目涧郊。
第一步 安裝
新建一個項目文件夾雷客,命名為MyProject
然后在文件夾里按住Shift
點擊鼠標右鍵搪柑,選擇在此處打開命令窗口。
在打開的窗口中輸入npm install express
和npm install ejs
去安裝他們和他們所需要的依賴辜腺。安裝完之后目錄中會多出一個node_modules
文件夾伴奥。
第二步 構建目錄
新建routes
文件夾盛杰,用于存放各頁面的路由文件
例如Demo中的index.js
文件
exports.index = function(req,res){
res.render("index",{title:'首頁'});
}
然后新建static
文件夾,用于存放頁面框架
例如Demo中的bootstrap
框架
接著再創(chuàng)建一個views
文件夾倦零,用于存放頁面文件
例如Demo中的index.ejs
文件
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3個meta標簽*必須*放在最前面够话,任何其他內(nèi)容都*必須*跟隨其后! -->
<title><%=title%></title>
<!-- Bootstrap -->
<link href="static/bootstrapcss/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>你好光绕,世界女嘲!</h1>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="static/bootstrapcss/js/bootstrap.min.js"></script>
</body>
</html>
最后再創(chuàng)建一個app.js
文件,也就是程序的入口文件诞帐。
var express = require("express");
var routes = require("./routes");
var app = express();
app.set("view engine",'ejs');
app.get("/",routes.index);
app.listen(8989);
console.log("espress start");
同樣的我們在MyProject
目錄調出命令行工具欣尼,并通過node app.js
命令來運行程序
這時我們在網(wǎng)頁端就可以通過輸入127.0.0.1:8989
來打開網(wǎng)站了
01.png