这是我的后端代码。它有 2 条受保护的路线。需要授权标头(承载令牌)。
我的后端没有任何问题。它正在与邮递员和移动应用程序配合使用。我认为前端有一个限制。
我不想
我想知道为什么 Bearer token 比 cookie 更受欢迎,如果我必须使用 cookie 来实现这一点。
后端
// other imports .....
const path = require('path');
const fs = require('fs');
const express = require('express');
const app = express();
app.use((req, res, next) => {
try {
const token = req.get('Authorization');
if(!token) {
throw new Error('401');
}
const userId = getIdFromToken(token);
if(!userId) {
throw new Error('401');
}
res.locals.userId = userId;
next();
} catch (error) …
Run Code Online (Sandbox Code Playgroud)