node express基本使用(一)

2022-06-11 01:15:13 字數 3828 閱讀 1575

這裡 用的express 5 請注意

1. 建立乙個新的路由器物件。

const router =express.router([options])

mergeparams

//保留來自父路由器的req.params值。如果父物件和子物件具有衝突的引數名,則以子物件的值為準。

casesensitive //

啟用區分大小寫。 預設情況下禁用,將「 / foo」和「 / foo」視為相同。

strict //

啟用嚴格路由。 預設情況下禁用,路由器將「 / foo」和「 / foo /」視為相同。

//很麻煩這樣寫

//可以建立乙個router.js 專門用來乙個路由匹配多個子路由

var router =express.router()

router.get("/",(req,res)=>)

router.get("/one",(req,res)=>)

router.get("/second",(req,res)=>)

module.exports =router;

//var express = require('express')

var router = require("./router")

router路由物件中的路由都會匹配到"/home"路由後面

//中介軟體也可以使用多級 callback anth()中要呼叫next() 否則不會往下走

//callback中定義的const let 在下乙個callback中是無法訪問的 要掛載到 req 或者 res 中

const model_name = require('inflection').classify(req.params.resourse)

//const model = require(`../../models/$`);

//為什麼不使用 const router 會訪問不到model 所以掛載到req上

req.model = require(`../../models/$`);

next()

}, router)

3. 載入靜態資源

const path = require('path')

//為了提供對靜態資源檔案(,css,js檔案)的服務,請使用express內建的中間函式express.static

uploads檔案中靜態資源通過 http://

localhost:3000/uploads/6abe1df86915ae97a3ee28537a5f8cfa 來訪問

//為了給靜態資源檔案建立乙個虛擬的檔案字首(檔案系統中不存在),可以使用express.static函式指定乙個虛擬的靜態目錄

//使用『/static』作為字首來載入public資料夾下的檔案了

4. 不同模組的介面分模組開發

5.  使用crud 共工介面

/*

* * 共享介面 crud

* 假如 有兩個模板 功能都是增刪改查 介面都是相似的操作 就可以使用共享介面

* 需要自己加乙個 rest 防止介面衝突

* 後面是動態的引數 也就是自己的模型名 資料庫表名 == 介面名

* req.params.resourse 可以拿到動態引數 也就是表名 resourse 是自己定義的 也可以是別的

* 然後 在介面裡面 引入引數對應的模型名

*/ const express = require('express')

const anth = require('../../utils/auth')

const router =express.router() //

建立express 的子路由, 分模組儲存介面

router.post('/', async(req, res) =>)

router.get('/', async(req, res) =>

if (req.model.modelname == 'category')

if (req.model.modelname == 'article')

const items = await req.model.find().setoptions(queryoption).limit(10) //

find 相當於select 進行查詢操作 populate 查詢繫結物件

res.send(items)

})router.get('/:id', async(req, res) =>)

router.put('/:id', async(req, res) =>)

})router.

delete('/:id', async(req, res) =>)

})//使用 rest 防止介面衝突 加上私有字首

///admin/api/rest/:resourse 中 /:resourse 繫結動態名稱 resourse 是自己定義的 也可以是別的

//前台傳得 http://localhost:3000/admin/api/rest/items

可以拿到動態引數 也就是表名 也就是前台介面中items

//*inflection外掛程式 inflection.classify( 'message_bus_properties' );//=== 'messagebusproperty' 會把單詞 複數 轉換成 單數 因為mongoose中表名是單數 而介面中都是複數 轉一下子

const model_name = require('inflection').classify(req.params.resourse)

//const model = require(`../../models/$`);

//為什麼不使用 const router 會訪問不到 model 所以掛載到req上

req.model = require(`../../models/$`);

next()

}, router)

}

6. 檔案上傳 中介軟體multer

const multer = require('multer')

//dest 目標位址 檔案存放在**

const upload = multer()

接受名稱為的單個檔案fieldname。單個檔案將儲存在中req.file。

const file =req.file

//如果想讓前台檢視上傳要拼接檔案路徑返給前台 file.filename就是儲存的檔名 uploads是儲存的資料夾

7. 對儲存使用者密碼進行加密解密操作 bcryptjs

const bcryptjs = require('bcryptjs')

//val 要加密的資料 也就是密碼 10 就要加密的等級 最好是10-12之間 12以後加密跟解密效能慢 但是安全度高

var pwd = bcryptjs'.hashsync(val, 10)

// 然後存到資料庫表中

// 登陸的時候 先根據前台傳的 name 去資料庫中那條資料 name 一般是唯一的 然後在取出密碼

// 密碼校驗的操作 password 是使用者傳來的密碼 user.password 是資料庫儲存的加密的密碼

var flag = bcryptjs.comparesync(password, user.password) // 返回 true 和 false

// true 返回正確 false 返回錯誤

使用node express後台服務

npm install g express generator全域性安裝express命令安裝工具 npm install g install express全域性安裝express express 專案名 e使用express建立新專案 cd 該目錄切換目錄 npm install npm sta...

Node Express 內容渲染

大多數情況下,渲染內容用 res.render,它最大程度地根據布局渲染檢視。如果想寫乙個快速測試頁,也許會用到 res.send。你可以使用 req.query 得到查詢字串的值,使用req.session 得到會話值,或使用req.cookie req.singedcookies 得到 cook...

Node express 官方例子cors

最近學習node以及express,看例子看的頭疼,剛看完cors,寫一下記錄下來。以下是index.js var express require var logger require morgan var bodyparser require body parser var api express...