CMPE280 group project development guide

Development Guide

For normal development, there are three places need to modify

views/pages
routes
app.js

Steps:

  1. Create HTML file (example.html) in views/pages directory

<html>
<head>
<title>Example page</title>
<% include ../partials/head %>
</head>
<body>
<% include ../partials/nav %>

<!-- write your own HTML codes -->

<% include ../partials/footer %>
<script type="text/javascript">
    // write jQuery codes here if needed
</script>

</body>
</html>
```

  1. Create router file (example.js) in routes folder
var express = require('express');
var router = express.Router();
module.exports = function () {
      router.get('/', function(req, res) {
        console.log("in example");
        res.render('pages/example'); // normally, we ignore the extension name of example.html file
      });

      return router;
};
  1. Connect those two files in app.js file
    Append var example = require('./routes/example')(); to the end of import block.
    This is for letting our app know we want to use our example.js module
import router module

After importing, we need to route URLs to our newly imported router.
Append app.use('/example', example); to the end of route block

This statement means we connect localhost:3000/example and other URLs under this base URL to this example router


Details

1. [Folder] routes

This folder contains router file. (javascript file)

Routers' purpose:
  1. routes request from user end to server
  2. handles data manipulation such as pre-processing data in order to simplify data loading process in HTML file
  3. renders HTML files which are corresponding to certain URLs
Template for router file

demo.js:

var express = require('express'); // import express module
var router = express.Router(); // initialize router instance from express

module.exports = function () { // export this module as a function
  router.get('/', function(req, res) { // router for GET request, in currnt relative root URL (we are in demo.js file, this router represents "localhost:3000/demo/" )
    res.render("pages/demo"); // render html page
  });

  router.get("/test", function (req, res) {
    // Do something when users visit "localhost:3000/demo/test" (GET request)
  });

  router.post("/", function (req, res) {
    // Do something when users send a POST request to "localhost:3000/demo/test"
      // For example, there is a form in demo.html page, when user submits that form, a POST request will be sent here 
  });

  return router; // return router instance
};

2. [Folder] views/pages

This folder contains normal HTML files.

Example for view file

demo.html

<html>
    <head>
        <% include ../partials/head %>
    </head>
    <body>
      <% include ../partials/nav %>
      <!-- form begin -->
      <form>
        <!-- Name input -->
        <div class="row"> 
          <div class="col-md-5">
            <div class="form-group">
              <label>Name</label>
              <input type="text" class="form-control border-input" id="idName" placeholder="Restaurant Name">
            </div>
          </div>
        </div>
        <!-- Address input -->
        <div class="row">
          <div class="col-md-12">
            <div class="form-group">
              <label>Address</label>
              <input type="text" class="form-control border-input" id="idAddress" placeholder="Address">
            </div>
          </div>
        </div>
        <!-- button -->
        <div class="text-center">
          <button type="button" class="btn btn-info" id="btnCreateRes">Create Restaurant</button>
        </div>
      </form>
      <% include ../partials/footer%>

      <script type="text/javascript">
        $(document).ready(function(){
            // on-click listener for creating restaurant
            $("#btnCreateRes").on("click", function () {
                // collect input value into data object
                var data = {
                    // read from each input
                    restaurantName: $("#idName").val(),
                    restaurantAddr: $("#idAddress").val()
                };
                // use jquery ajax to send post request to router, along with data as parameter
                // this post request will be sent to demo.js, routed to the cooresponding url
                $.post("/demo", data, function (res) {
                    // call back function
                })
            });
        });
      </script>
    </body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末村刨,一起剝皮案震驚了整個(gè)濱河市峭火,隨后出現(xiàn)的幾起案子睁宰,更是在濱河造成了極大的恐慌樱拴,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件畔濒,死亡現(xiàn)場(chǎng)離奇詭異剩晴,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)侵状,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門赞弥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人壹将,你說我怎么就攤上這事嗤攻。” “怎么了诽俯?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵妇菱,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我暴区,道長(zhǎng)闯团,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任仙粱,我火速辦了婚禮房交,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘伐割。我一直安慰自己候味,他們只是感情好刃唤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著白群,像睡著了一般尚胞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上帜慢,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天笼裳,我揣著相機(jī)與錄音,去河邊找鬼粱玲。 笑死躬柬,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的抽减。 我是一名探鬼主播允青,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼胯甩!你這毒婦竟也來了昧廷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤偎箫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后皆串,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體淹办,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年恶复,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了怜森。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谤牡,死狀恐怖副硅,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情翅萤,我是刑警寧澤恐疲,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站套么,受9級(jí)特大地震影響培己,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜胚泌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一省咨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧玷室,春花似錦零蓉、人聲如沸笤受。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)感论。三九已至,卻和暖如春紊册,著一層夾襖步出監(jiān)牢的瞬間比肄,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工囊陡, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留芳绩,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓撞反,卻偏偏與公主長(zhǎng)得像妥色,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子遏片,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容