《Node + Express 快速搭建網(wǎng)站》學(xué)習(xí)筆記-7刪除功能及項(xiàng)目生成配置文件

4-1刪除功能及項(xiàng)目生成配置文件

ch4-刪除功能及項(xiàng)目生成配置文件

修改項(xiàng)目結(jié)構(gòu)

靜態(tài)文件目前存儲(chǔ)在bower_components下面郎仆,但是與自定義靜態(tài)文件不便于集中管理。
可以利用bower指定安裝的靜態(tài)文件目錄的功能

.bowerrc 配置文件

{
    "directory": "public/libs"
}

刪除“./bower_compinents/”目錄

bower install bootstrap 

更改head模板靜態(tài)資源路徑

編寫(xiě)數(shù)據(jù)庫(kù)交互(刪)代碼

app.js

...

//list delete movie
app.delete('/admin/list', function(req, res){
    var id = req.query.id;
    if(id){
        Movie.remove({_id: id}, function(err, movie){
            if(err){
                console.log(err);
            }else{
                res.json({success: 1});
            }
        });
    }
})

在list.js中添加事件代理js

extend ../layout

block content
    .container
        .row
            table.table.table-hover.table-bordered
                thead
                    tr
                        th 電影名字
                        th 導(dǎo)演
                        th 國(guó)家
                        th 上映年份
                        //- th 錄入時(shí)間
                        th 查看
                        th 更新
                        th 刪除
                    tbody
                        each item in movies
                            tr(class="item-id-#{item._id}")
                                td #{item.title}
                                td #{item.doctor}
                                td #{item.country}
                                td #{item.year}
                                //- td #{moment(item.meta.createedAt).format('MM/DD/YYYY')}
                                td: a(target="_blank", href="../movie/#{item._id}") 查看
                                td: a(target="_blank", href="../movie/update/#{item._id}") 修改
                                td
                                    button.btn.btn-danger.del(type="button", data-id="#{item._id}") 刪除

    script(src="/js/admin.js")

/public/js/admin.js林说,處理邏輯

$(function(){
    $('.del').click(function(e){
        var target = $(e.target);
        var id = target.data('id');
        var tr = $('.item-id-' + id);

        $.ajax({
            type: 'DELETE',
            url: '/admin/list?id=' + id,

        }).done(function(results){
            if(results.success === 1){
                if(tr.length > 0){
                    tr.remove();
                }
            }
        });
    })
})

至此替废,前后端邏輯厘线,包括數(shù)據(jù)庫(kù)都已經(jīng)全部實(shí)現(xiàn)鸳君。

最后牲阁,需要對(duì)前端、后端所依賴的模塊和庫(kù)的版本進(jìn)行鎖定负敏,生成一個(gè)配置文件

生成配置文件

配置前端的靜態(tài)資源 bower init

iMooc-temp Zelda$ bower init
? name iMooc
? description demo-node-express
? main file
? keywords imooc node express
? authors zelda <ze.zh@hotmail.com>
? license MIT
? homepage
? would you like to mark this package as private which prevents it from being accidentally published to the registry? Yes
cidentally published to the registry? (y/N) y
{identally published to the registry? (y/N)
  name: 'iMooc',
  authors: [
    'zelda <ze.zh@hotmail.com>'
  ],
  description: 'demo-node-express',
  main: '',
  keywords: [
    'imooc',
    'node',
    'express'
  ],
  license: 'MIT',
  homepage: '',
  private: true,
  ignore: [
    '**/.*',
    'node_modules',
    'bower_components',
    'public/libs',
    'test',
    'tests'
  ],
  dependencies: {
    bootstrap: '^3.3.7'
  }
}

? Looks good? Yes

配置好之后就會(huì)生成bower.json配置文件

{
  "name": "iMooc",
  "authors": [
    "zelda <ze.zh@hotmail.com>"
  ],
  "description": "demo-node-express",
  "main": "",
  "keywords": [
    "imooc",
    "node",
    "express"
  ],
  "license": "MIT",
  "homepage": "",
  "private": true,
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "public/libs",
    "test",
    "tests"
  ],
  "dependencies": {
    "bootstrap": "^3.3.7"
  }
}

配置后端依賴 npm init

iMooc-temp Zelda$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (iMooc-temp) iMooc
Sorry, name can no longer contain capital letters.
name: (iMooc-temp) imooc
version: (1.0.0) 0.0.1
description: demo
entry point: (app.js)
test command:
git repository:
keywords: imooc node express
author: zelda
license: (ISC)
About to write to /Users/Zelda/Dev/demo/iMooc-temp/package.json:

{
  "name": "imooc",
  "version": "0.0.1",
  "description": "demo",
  "main": "app.js",
  "dependencies": {
    "body-parser": "^1.17.1",
    "express": "^4.15.2",
    "jade": "^1.11.0",
    "moment": "^2.17.1",
    "mongoose": "^4.9.0",
    "serve-static": "^1.12.1",
    "underscore": "^1.8.3"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "imooc",
    "node",
    "express"
  ],
  "author": "zelda",
  "license": "ISC"
}


Is this ok? (yes) y
iMooc-temp Zelda$

配置好之后生成package.json配置文件

{
  "name": "imooc",
  "version": "0.0.1",
  "description": "demo",
  "main": "app.js",
  "dependencies": {
    "body-parser": "^1.17.1",
    "express": "^4.15.2",
    "jade": "^1.11.0",
    "moment": "^2.17.1",
    "mongoose": "^4.9.0",
    "serve-static": "^1.12.1",
    "underscore": "^1.8.3"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "imooc",
    "node",
    "express"
  ],
  "author": "zelda",
  "license": "ISC"
}

下一步:

  • 電影分類(lèi)
  • 角色贡茅、權(quán)限管理
  • 還未集成到Grunt自動(dòng)重啟服務(wù)等
  • 單元測(cè)試的實(shí)現(xiàn)

高級(jí)課程見(jiàn):http://www.imooc.com/learn/197

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市其做,隨后出現(xiàn)的幾起案子顶考,更是在濱河造成了極大的恐慌,老刑警劉巖庶柿,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件村怪,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡浮庐,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)柬焕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)审残,“玉大人,你說(shuō)我怎么就攤上這事斑举〗两危” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵富玷,是天一觀的道長(zhǎng)璧坟。 經(jīng)常有香客問(wèn)我既穆,道長(zhǎng),這世上最難降的妖魔是什么雀鹃? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任幻工,我火速辦了婚禮,結(jié)果婚禮上黎茎,老公的妹妹穿的比我還像新娘囊颅。我一直安慰自己,他們只是感情好傅瞻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布踢代。 她就那樣靜靜地躺著,像睡著了一般嗅骄。 火紅的嫁衣襯著肌膚如雪胳挎。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,598評(píng)論 1 305
  • 那天溺森,我揣著相機(jī)與錄音慕爬,去河邊找鬼。 笑死儿惫,一個(gè)胖子當(dāng)著我的面吹牛澡罚,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播肾请,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼留搔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了铛铁?” 一聲冷哼從身側(cè)響起隔显,我...
    開(kāi)封第一講書(shū)人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎饵逐,沒(méi)想到半個(gè)月后括眠,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡倍权,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年掷豺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片薄声。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡当船,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出默辨,到底是詐尸還是另有隱情德频,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布缩幸,位于F島的核電站壹置,受9級(jí)特大地震影響竞思,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜钞护,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一盖喷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧患亿,春花似錦传蹈、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至咙冗,卻和暖如春沾歪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背雾消。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工灾搏, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人立润。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓狂窑,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親桑腮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子泉哈,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,146評(píng)論 25 707
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)破讨,斷路器丛晦,智...
    卡卡羅2017閱讀 134,657評(píng)論 18 139
  • 唯唯_c1fc閱讀 364評(píng)論 0 3
  • When Michael finished telling the story, he looked around...
    一文一樹(shù)閱讀 227評(píng)論 0 0
  • 學(xué)習(xí)為的是改變,改變讓人成長(zhǎng)提陶。當(dāng)一個(gè)非常熟悉的人發(fā)生180度改變的時(shí)候烫沙,又是另一種全新的成長(zhǎng)開(kāi)始。學(xué)則改變隙笆,思則如...
    呂明超閱讀 193評(píng)論 0 0