一個簡單的node項目

koa ---基于node.js 平臺是 下一代 web 開發(fā)框架

  • 安裝 npm install express -save
EJS 模板 <%= EJS %>
  • 安裝 npm install ejs -S

  • https://ejs.bootcss.com/

  • "E" 代表 "effective"障涯,即【高效】。EJS 是一套簡單的模板語言,幫你利用普通的 JavaScript 代碼生成 HTML 頁面。EJS 沒有如何組織內(nèi)容的教條;也沒有再造一套迭代和控制流語法解虱;有的只是普通的 JavaScript 代碼而已。

  1. 重新創(chuàng)建 app.js

    // path是原生模塊,不需要安裝
    const path = require('path')
    ?
    const app = express()
    ?
    // 設(shè)置默認的views的目錄
    app.set('views', path.resolve(__dirname, 'views'))
    // 設(shè)置渲染引擎為ejs, 得保證你已經(jīng)安裝ejs
    app.set('view engine', 'ejs')
    ?
    app.get('/', (req, res) => {
     res.render('index')
    })
    ?
    app.listen(3000, () => {
     console.log('server is running on http://localhost:3000')
    })</pre>
    
    
  2. 新建文件夾 views

    • 新建 index.ejs 支持html語法

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n218" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;"><!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title><%= title %></title>
      </head>
      <body>
      <%- include('nav'); %>
      <img src="/liuhao/images/1.jpg" alt="1">
      hello ejs express home
      </body>
      </html></pre>

    • 在app.js 里面使用path方法

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" cid="n221" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">const express = require('express')
    // path是原生模塊墨坚,不需要安裝
    const path = require('path')
    ?
    const app = express()
    ?
    // 設(shè)置默認的views的目錄
    app.set('views', path.resolve(__dirname, 'views'))
    // 設(shè)置渲染引擎為ejs, 得保證你已經(jīng)安裝ejs
    app.set('view engine', 'ejs')
    ?
    app.get('/', (req, res) => {
    //res.render('index')
    app.get('/', (req, res) => {
    res.render('index', {
    title: 'ejs and express are very niubility'
    })
    })
    })
    ?
    app.listen(3000, () => {
    console.log('server is running on http://localhost:3000')
    })</pre>

    訪問 http://localhost:3000/ 可以看到 title 內(nèi)容

    • 新建 about.ejs

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" cid="n226" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;"><!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title><%= title %></title>
      </head>
      <body>
      <%- include('nav'); %>
      about page
      </body>
      </html></pre>

    • 更改 app.js 添加 about

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" cid="n229" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">//引入安裝的express
      const express = require('express');
      //path是原生模塊,不需要安裝
      const path = require('path');
      ?
      const app = express();
      ?
      //當前文件所在的目錄
      console.log(__dirname);
      //命令開始執(zhí)行的目錄
      console.log(process.cwd());
      ?
      //靜態(tài)資源目錄,設(shè)置默認的views目錄
      app.set('views', path.resolve(__dirname, 'views'))
      // 設(shè)置渲染引擎為ejs, 得保證你已經(jīng)安裝ejs
      app.set('view engine', 'ejs')
      ?
      app.get('/', (req, res) => {
      //可以找到exprs所有內(nèi)容
      //console.log(req)
      res.render('index', {
      //服務(wù)器代碼
      title: 'ejs and express are very niubility'
      })
      })
      ?
      app.get('/about', (req, res) => {
      res.render('about', {
      title: 'about'
      })
      })
      ?
      app.listen(3000, () => {
      console.log('server is running on http://localhost:3000')
      })</pre>

      訪問 localhost:3000/about

  3. 添加導(dǎo)航條

    • 修改 views的 indxe.ejs

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n236" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;"><!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title><%= title %></title>
      </head>
      <body>

      <%- include('nav'); %>
      <nav>
      <a href="/">首頁</a>
      <a gref="/about">關(guān)于</a>
      </nav>

      hello ejs express home
      </body>
      </html></pre>

  4. 創(chuàng)建一個nav.ejs , 測試連接 訪問localhost:3000

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n239" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;"><div>
    測試連接 cddxx
    </div></pre>

  5. 更新 about.ejs

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" cid="n242" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;"><!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title><%= title %></title>
    </head>
    <body>
    <%- include('nav'); %>
    about page
    </body>
    </html></pre>

  6. 新建文件夾 assets

    • 新建 image 放照片
  7. 在 index.ejs 里面添加 images 圖片

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" cid="n251" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;"><!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title><%= title %></title>
    </head>
    <body>

    <%- include('nav'); %>
    <img src="/images/1.jpg" alt="1"/>
    <nav>
    <a href="/">首頁</a>
    <a gref="/about">關(guān)于</a>
    </nav>

    hello ejs express home
    </body>
    </html></pre>

  8. 因為在express 里面出于一些安全性的考慮映挂,會理解為靜態(tài)目錄

    • 設(shè)置靜態(tài) app.js

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" cid="n257" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: inherit; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit;">//引入安裝的express
    const express = require('express');
    //path是原生模塊泽篮,不需要安裝
    const path = require('path');
    ?
    const app = express();
    ?
    //當前文件所在的目錄
    console.log(__dirname);
    //命令開始執(zhí)行的目錄
    console.log(process.cwd());
    ?
    //靜態(tài)資源目錄,設(shè)置默認的views目錄
    app.set('views', path.resolve(__dirname, 'views'))
    // 設(shè)置渲染引擎為ejs, 得保證你已經(jīng)安裝ejs
    app.set('view engine', 'ejs')
    ?
    // 設(shè)置靜態(tài)資源目錄 盗尸, static假的目錄
    //寫了的話,也需要在indxe.ejs里面的img 里面添加 /static
    app.use('/static', express.static(path.resolve(__dirname, 'assets')))
    ?
    app.get('/', (req, res) => {
    //可以找到exprs所有內(nèi)容
    //console.log(req)
    res.render('index', {
    //服務(wù)器代碼
    title: 'ejs and express are very niubility'
    })
    })
    ?
    app.get('/about', (req, res) => {
    res.render('about', {
    title: 'about'
    })
    })
    ?
    app.listen(3000, () => {
    console.log('server is running on http://localhost:3000')
    })</pre>

  9. 設(shè)置公共 nav.ejs 再修改

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="js" cid="n260" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><nav>
    <a href="/">首頁</a>
    <a gref="/about">關(guān)于</a>
    </nav></pre>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
禁止轉(zhuǎn)載帽撑,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者振劳。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市油狂,隨后出現(xiàn)的幾起案子历恐,更是在濱河造成了極大的恐慌,老刑警劉巖专筷,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弱贼,死亡現(xiàn)場離奇詭異,居然都是意外死亡磷蛹,警方通過查閱死者的電腦和手機吮旅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來味咳,“玉大人庇勃,你說我怎么就攤上這事〔凼唬” “怎么了责嚷?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長掂铐。 經(jīng)常有香客問我罕拂,道長,這世上最難降的妖魔是什么全陨? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任爆班,我火速辦了婚禮,結(jié)果婚禮上辱姨,老公的妹妹穿的比我還像新娘柿菩。我一直安慰自己,他們只是感情好雨涛,可當我...
    茶點故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布枢舶。 她就那樣靜靜地躺著,像睡著了一般镜悉。 火紅的嫁衣襯著肌膚如雪祟辟。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天侣肄,我揣著相機與錄音旧困,去河邊找鬼。 笑死,一個胖子當著我的面吹牛吼具,可吹牛的內(nèi)容都是我干的僚纷。 我是一名探鬼主播,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼拗盒,長吁一口氣:“原來是場噩夢啊……” “哼怖竭!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起陡蝇,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤痊臭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后登夫,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體广匙,經(jīng)...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年恼策,在試婚紗的時候發(fā)現(xiàn)自己被綠了鸦致。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡涣楷,死狀恐怖分唾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情狮斗,我是刑警寧澤绽乔,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站情龄,受9級特大地震影響迄汛,放射性物質(zhì)發(fā)生泄漏捍壤。R本人自食惡果不足惜骤视,卻給世界環(huán)境...
    茶點故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望鹃觉。 院中可真熱鬧专酗,春花似錦、人聲如沸盗扇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽疗隶。三九已至佑笋,卻和暖如春斑鼻,著一層夾襖步出監(jiān)牢的瞬間蒋纬,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留关摇,地道東北人输虱。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓蚕钦,卻偏偏與公主長得像命贴,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子食听,可洞房花燭夜當晚...
    茶點故事閱讀 43,658評論 2 350

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