所以我有這個自定義中間件,它試圖從客戶端獲取 cookie。這是宣告:
function auth(req, res, next) {}
但是要從請求中訪問 c??ookie,我需要使用另一個中間件,即 cookieParser。所以現在我不知道如何在我的自定義身份驗證中間件中使用該 cookieParser。任何幫助將不勝感激。
uj5u.com熱心網友回復:
您不在中間件中使用中間件。請參閱下面的代碼,這應該是不言自明的,但我添加了一些評論。
var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
// add the cookie parser
app.use(cookieParser());
// your middleware
app.use(function auth(req, res, next) {
// the cookie middleware above has processed the cookies
// you access them like this
console.log(req.cookies);
});
app.get('/', ...);
app.listen(8080)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/432979.html
上一篇:何時在node.js中使用app.get("/",fun(res,req))和app.post("/",fun(res,req))?
下一篇:命名變數顯示為字串
