Node.js 中 cookie-parser 依賴安裝使用詳解
解析 HTTP 請(qǐng)求中的 Cookie 的 Node.js 中間件
一、安裝
npm install cookie-parser
二、基本使用
const express = require("express"); const cookieParser = require("cookie-parser"); const app = express(); // 使用 cookie-parser 中間件 app.use(cookieParser()); app.get("/", (req, res) => { // 獲取請(qǐng)求中的 Cookie const cookies = req.cookies; console.log("Cookies:", cookies); res.send("Cookie parsed successfully"); }); const port = 3000; app.listen(port, () => { console.log(`Server running on port ${port}`); });
二、解析與設(shè)置 Cookie
1. 解析 Cookie
`cookie-parser` 會(huì)把請(qǐng)求中的 Cookie 解析成一個(gè)對(duì)象,存儲(chǔ)在 `req.cookies` 中。可以通過訪問 `req.cookies` 來獲取 Cookie 的值。
app.get("/get-cookie", (req, res) => { const username = req.cookies.username; if (username) { res.send(`Hello, ${username}`); } else { res.send("No username cookie found"); } });
2. 設(shè)置 Cookie
使用 `res.cookie()` 方法可以設(shè)置響應(yīng)中的 Cookie。該方法接受三個(gè)參數(shù):Cookie 的名稱、值和可選的配置對(duì)象。
app.get("/set-cookie", (req, res) => { // 設(shè)置一個(gè)名為 username 的 Cookie,值為 John,有效期為 1 小時(shí) res.cookie("username", "John", { maxAge: 3600000, httpOnly: true }); res.send("Cookie set successfully"); });
3. 簽名 Cookie
`cookie-parser` 支持對(duì) Cookie 進(jìn)行簽名,以確保 Cookie 的完整性和安全性。在初始化 `cookie-parser` 時(shí)傳入一個(gè)密鑰,就可以使用簽名 Cookie。
const express = require("express"); const cookieParser = require("cookie-parser"); const app = express(); // 使用帶有密鑰的 cookie-parser 中間件 app.use(cookieParser("mysecretkey")); app.get("/set-signed-cookie", (req, res) => { // 設(shè)置一個(gè)簽名的 Cookie res.cookie("signedUsername", "Jane", { signed: true }); res.send("Signed cookie set successfully"); }); app.get("/get-signed-cookie", (req, res) => { // 獲取簽名的 Cookie const signedUsername = req.signedCookies.signedUsername; if (signedUsername) { res.send(`Hello, ${signedUsername}`); } else { res.send("No signed username cookie found"); } }); const port = 3000; app.listen(port, () => { console.log(`Server running on port ${port}`); });
三、清除 Cookie
使用 `res.clearCookie()` 方法可以清除客戶端的 Cookie。需要指定要清除的 Cookie 的名稱。
app.get("/clear-cookie", (req, res) => { res.clearCookie("username"); res.send("Cookie cleared successfully"); });
到此這篇關(guān)于Node.js 中 cookie-parser 依賴詳解的文章就介紹到這了,更多相關(guān)Node.js cookie-parser 依賴內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nodejs實(shí)現(xiàn)黑名單中間件設(shè)計(jì)
最近寫的項(xiàng)目中涉及到防止灌水的功能,于是設(shè)計(jì)了黑名單中間件,跟大家分享一下,同時(shí)也希望大家有好的建議能夠拍磚.2014-06-06node.js 開發(fā)指南 – Node.js 連接 MySQL 并進(jìn)行數(shù)據(jù)庫操作
通常在NodeJS開發(fā)中我們經(jīng)常涉及到操作數(shù)據(jù)庫,尤其是 MySQL ,作為應(yīng)用最為廣泛的開源數(shù)據(jù)庫則成為我們的首選,本篇就來介紹下如何通過NodeJS來操作 MySQL 數(shù)據(jù)庫。2014-07-07前端自動(dòng)化開發(fā)之Node.js的環(huán)境搭建教程
這篇文章主要介紹了前端自動(dòng)化開發(fā)之Node.js環(huán)境搭建的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用node.js具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04node.js調(diào)用Chrome瀏覽器打開鏈接地址的方法
其實(shí)在Node.JS中打開瀏覽器網(wǎng)址非常簡(jiǎn)單,但還是有必要整理下分享給有需要的朋友們,這篇文章主要給大家介紹了node.js如何調(diào)用Chrome瀏覽器打開鏈接地址的方法,文中介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起看看吧。2017-05-05如何從頭實(shí)現(xiàn)一個(gè)node.js的koa框架
這篇文章主要介紹了如何從頭實(shí)現(xiàn)一個(gè)node.js的koa框架,koa.js是最流行的node.js后端框架之一,有很多網(wǎng)站都使用koa進(jìn)行開發(fā),同時(shí)社區(qū)也涌現(xiàn)出了一大批基于koa封裝的企業(yè)級(jí)框架。,需要的朋友可以參考下2019-06-0610個(gè)Node.js庫幫助你優(yōu)化代碼和簡(jiǎn)化開發(fā)
這篇文章主要介紹了10個(gè)Node.js庫幫助你優(yōu)化代碼和簡(jiǎn)化開發(fā),其中包括處理數(shù)組、對(duì)象、字符串庫Lodash,緩存數(shù)據(jù)處理庫Node-cache,解析、操作和格式化日期和時(shí)間庫Moment.js,Redis操作庫,發(fā)送電子郵件庫Nodemailer2023-05-05