Express Cookie 解析器 – 带签名和未签名 Cookie


我们可以使用 Cookie 在客户端的 Web 浏览器中存储用户信息。例如,每当用户在浏览器中打开任何网站时,它都会向服务器请求一些信息,并且一旦客户端获取到信息,就会将其与过期时间一起存储在浏览器中。

这使得网站对用户来说速度更快,因为它不需要每次用户返回网站时都向服务器请求信息。但是,一旦 Cookie 过期,客户端会再次向服务器请求该信息。

在本教程中,我们将学习 express cookie-parse npm 包以及如何在客户端的 Web 浏览器上设置 Cookie。

使用 cookie-parser NPM 包设置和获取 Cookie

用户可以按照以下步骤创建节点项目。

  • 步骤 1 − 首先,用户需要在本地计算机上下载并安装 Node Js。

  • 步骤 2 − 为项目创建一个新文件夹,并在该目录中打开终端。

  • 步骤 3 − 要启动新的节点项目,请在项目目录中的终端中输入以下命令。

npm init -y
  • 步骤 4 − 现在,用户需要安装所需的 npm 包。第一个需要的包是 express,另一个是 cookie-parser。用户可以执行以下命令将 npm 包下载到当前项目。

npm i express cookie-parser
  • 步骤 5 − 接下来,我们需要为节点服务器设置服务器代码。

用户可以在项目目录中创建一个新的 server.js 文件并添加以下代码。

// importing the express package
const express = require("express");

// using the express for app
const app = express();
app.get("/", (req, res) => {
   res.send("Welcome to the new server!");
});

// setting up the port for the server
app.listen(5000, (err) => {
   console.log("Server started successfully on port 5000");
});
  • 步骤 6 − 现在,用户需要运行项目,为此,用户可以在终端中输入以下命令。

node server.js

服务器已成功启动,当用户访问 https://:5000/ URL 时,他们可以看到以下输出。

我们已经设置了节点项目。我们需要编写代码来使用 cookie 解析器 NPM 包用于带签名和未签名 Cookie。

cookie-parse 包的方法

在开始使用 Cookie 解析器编写 Cookie 代码之前,让我们了解它包含的方法。

cookieParser(key, cookieOptions)

用户可以使用 cookieParser() 方法创建一个新的中间件。它将密钥和 Cookie 选项作为参数;但是,两者都是可选的。如果我们传递一个密钥,它会将 Cookie 解析为带签名的 Cookie;否则,作为未签名的 Cookie。

此外,用户可以使用 request.cookie 解析未签名的 Cookie,并使用 request.signedCookies 解析带签名的 Cookie。

cookieParser.JSONCookie(string)

如果我们设置一个 JSON 对象作为 Cookie,JSONCookie() 方法将返回 JSON 对象;否则,返回普通的字符串值。

cookieParser.JSONCookies(Cookies_to_store)

Cookies_to_store 是一个对象。因此,当我们将对象作为 JSONCookies() 方法的参数传递时,它会遍历对象的每个键值对,解析每个键的值,并将其替换为解析后的值。

cookieParser.signedCookie(string, key)

我们可以使用 signedCookie() 方法获取解析后的未签名 Cookie。如果密钥无效,该方法会引发错误。此外,如果 Cookie 未签名,则该方法会返回普通的未签名值。

cookieParser.signedCookies(Cookies_to_store, key)

在这里,Cookies_to_Store 再次是一个对象,用户可以使用 signedCookies() 方法遍历对象的每个值,并在密钥有效的情况下为每个值解析带签名的 Cookie。它会返回一个包含解析后值的新 Cookie 对象。

注意 − 用户只有在服务器处于生产模式下才能看到 Cookie 输出。此外,用户可以使用在线 nodeJS 编辑器查看输出。

示例 – 未签名 Cookie

在下面的示例中,我们向浏览器发送 Cookie,而无需使用 Cookie 解析器中间件。我们使用了 send() 方法将 Cookie 发送到浏览器。

const express = require("express");
const parser = require("cookie-parser");
const app = express();
app.get("/cookie", (request, response) => {

   // sending the unassigned cookies
   response.cookie("message", "This is a cookie.").send();
   console.log("The cookies are " + request.cookies);
});
app.listen(5000, (err) => {
   console.log("Server started successfully on port 5000");
});

示例 - 带签名 Cookie

在下面的示例中,我们使用了 Cookie 解析器中间件来生成带签名的密钥。用户可以看到我们如何使用 secret_key 初始化中间件。之后,我们将 {signed: true } 对象作为 cookie() 方法的参数添加,以发送带签名的 Cookie。

const express = require("express");
const parser = require("cookie-parser");
const app = express();

// Initializing the middleware with a secret key
app.use(parser("secret_key"));

app.get("/cookie", (request, response) => {
   // sending the signed cookies
   response
   .cookie("message", "This is a singed cookie", { signed: true })
   .send();
   
   // using the signedCookies property to access cookies
   console.log("The cookies are " + request.signedCookies);
});
app.listen(5000, (err) => {
   console.log("Server started successfully on port 5000");
});

在本教程中,我们学习了如何发送带签名和未签名的 Cookie。密钥是带签名和未签名 Cookie 之间的主要区别。我们为带签名的 Cookie 提供密钥,并且需要使用它来访问它们。

要设置未签名的 Cookie,用户不需要使用 Cookie 解析器作为带有密钥的中间件。

更新于: 2023 年 3 月 17 日

1K+ 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.