nodejs-响应中存在Set-Cookie,但在浏览器中缺失

时间:2019-10-06 13:37:10

标签: javascript express header passport.js

我正在使用express.js和passport.js作为我的支持,并以axios和vue.js作为前端。

我可以看到我的set-cookie,但该cookie在我的浏览器中不存在。下面链接的图片。

Response headers Request headers

此cookie用于使用password.js对我的用户进行身份验证。

以下是我的express.js的代码。

var app = express();
app.use(cors({
  origin: "http://localhost:8080",
  credentials: true
}));

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({
  secret: 'p@ssw0rd'
}));
app.use(passport.initialize());
app.use(passport.session());

下面的方法将通过vue.js方法调用。

const url = "http://localhost:3000/";

let axiosConfig = {
  withCredentials: true,
  headers: {
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': 'http://localhost:3000/',
    'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
  }
}

class user {
  static login(username, password) {
    return new Promise(async(resolve, reject) => {
      try {
        const res = await axios.post(
          `${url}login`, {
            username,
            password
          },
          axiosConfig
        );
        resolve(res.data);
      } catch (err) {
        reject(err);
      }
    });
  }

后端运行在localhost:3000,前端运行在localhost:8080

检查cookie。 No data presented for selected host

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案,解决方案确实很愚蠢,没有代码相关。 由于我正在使用expressjs,而一个朋友正在使用vuejs,因此他添加了两个相同的文件夹,其中包含axios方法。我更改了未引用的部分。

我在问题中发布的代码有效。