nodejs--express開發個人部落格(-)

來源:互聯網
上載者:User

       寫完了入門筆記,開始進入開發階段吧。基於上一節的內容,現在著手開發個人部落格系統。先劃分一下功能吧 /:首頁 /login:登陸 /reg:註冊 /post:發表文章 /logout:退出 首先規劃一下路由控制,在現在的app.js中控制路由的語句是 [javascript]  app.get('/', routes.index);  app.get('/users', user.list);//路由控制  每加一個路由控制就要接一句app.get()或者是app.post(),當路由很多時,app.js裡面的代碼就會很多,不便於維護和修改。所以換一種寫法,在index.js裡面寫路由控制:[javascript] view plaincopyexports.index = function(req, res){    res.render('index', { title: 'Express' });  };  同時把app.js裡面的路由控制語句換成:routes(app); 這是你現在看到的寫法,咱們為了簡化和便於修改,換成下面這種寫法: [javascript]  module.exports = function(app){         app.get('/',function(req,res){});  }  部落格系統中的index.js的雛形: [javascript]  module.exports = function(app){          app.get('/',function(req,res){          res.render('index', { title: '首頁' });      });      app.get('/reg',function(req,res){          res.render('reg', { title: '註冊' });      });      app.post('/reg',function(req,res){      });      app.get('/login',function(req,res){          res.render('login', { title: '登入' });      });      app.post('/login',function(req,res){      });      app.get('/logout',function(req,res){      });      app.get('/post',function(req,res){          res.render('post', { title: '發表' });      });      app.post('/post',function(req,res){      });   };  你會發現reg、login和post都有get和post方法,我們可以這麼理解:get方法是實現當使用者試圖訪問這個網頁時要顯示些什麼,post方 法是當從這個網頁上發出資料(這裡時提交表單)時要幹些什麼,所以訪問/和/logout就不需要post方法了。render函數的意思是,當你要訪問 比如首頁時,伺服器找到index.ejs檔案並替換變數title的值為字串’首頁’ 路由控制的雛形出來了,下面寫視圖部分,在views下面建立幾個檔案,index.ejs、login.ejs、reg.ejs、post.ejs,然後再給每個頁面添加統一的頭部導航(header.ejs)和底部說明(footer.ejs),使用的時候把<% -include header%>和<% -include footer%>分別放在頭尾就行了。header.ejs [html] <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Blog</title><link rel="stylesheet" href="stylesheets/style.css"></head><body>    <header><h1><%= title %></h1></header>  <nav><span><a title="首頁" href="/">首頁</a></span><span><a title="登入" href="/login">登入</a></span><span><a title="註冊" href="/reg">註冊</a></span></nav><article>  footer.ejs </article></body></html> index.ejs [html]  <%- include header %>  這是首頁  <%- include footer %>  login.ejs[html] <%- include header %>  <form method="post" name="form" id="form">       <table>            <tbody>            <tr>                 <td><label for="username">使用者名稱:</label></td>                 <td><input type="text" id="username" name="username" placeholder="請輸入使用者名稱"/></td>            </tr>            <tr>                 <td><label for="password">密碼:</label></td>                 <td><input type="password" id="password" name="password" placeholder="請輸入密碼"/></td>            </tr>            <tr><td colspan="2" class="btn" ><input type="submit" value="登入"/></td></tr>            </tbody>       </table>  </form>  <%- include footer %>   reg.ejs[html]  <%- include header %>  <form method="post" name="form" id="form">       <table>            <tbody>            <tr>                 <td><label for="username">使用者名稱:</label></td>                 <td><input type="text" id="username" name="username" placeholder="請輸入使用者名稱"/></td>            </tr>            <tr>                 <td><label for="password">密碼:</label></td>                 <td><input type="password" id="password" name="password" placeholder="請輸入密碼"/></td>            </tr>            <tr>                 <td><label for="comfirm_pwd">再次輸入密碼:</label></td>                 <td><input type="password" id="comfirm_pwd" name="comfirm_pwd" placeholder="請再次輸入密碼"/></td>            </tr>            <tr><td colspan="2" class="btn"><input type="submit" value="註冊"/></td></tr>            </tbody>       </table>  </form>  <%- include footer %>   然後再修改下style.css:[css]  nav a:hover{      background: #2ECC71;  }  tr:last-child{      text-align: center;  }  td{      height: 24px;      padding: 5px;  }  td:first-child{      text-align: right;  }  td:last-child{      text-align: left;  }  td input[type="password"],td input[type="text"]{      display: block;      padding: 5px;      border: 1px solid gray;      border-radius: 3px;  }  td.btn{      text-align: center;        }  td.btn input{      padding:0 10px;      font-size: 16px;      font-weight: bold;      margin-top: 5px;  }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.