使用Nuxt auth和axios时Javascript堆内存不足

问题描述

我正在尝试在使用Express服务器中间件的Nuxt应用中实现本地登录

我正在使用以下模块

  1. nuxt身份验证
  2. axios
  3. 护照,本地护照,本地猫鼬,护照课程
  4. express-session

但是,一旦我在应用程序中设置了nuxt auth,整个应用程序似乎就会停止响应。我无法访问应用程序的任何页面(甚至登录注册页面)。 在运行服务器一段时间后,这就是我看到的。 https://i.stack.imgur.com/pl0Li.png

auth: {
redirect: {
  login: "/login",// User will be redirected to this path if login is required.
  home: "/",// User will be redirect to this path after login. (rewriteRedirects will rewrite this path)
  logout: "/login",// User will be redirected to this path if after logout,current route is protected.
  user: "/user",callback: "/",},strategies: {
  local: {
    endpoints: {
      login: {
        url: "/api/login",method: "post",propertyName: "token",logout: { url: "/api/logout",method: "post" },user: { url: "/",method: "get",propertyName: "user" },tokenrequired: true,//True by default
    tokenType: "bearer",// globalToken: true,// autoFetchUser: true
  },

},

根据https://github.com/nuxt-community/auth-module/issues/286#issuecomment-461138282,向服务器中间件发出请求是否可以导致此错误

我应该为api使用单独的服务器吗?

解决方法

我正面临着同样的问题。就我而言,问题在于以下身份验证配置。它是在全新安装的nuxt:2.14.3上发生的,因此应该很容易重现该错误。仅添加此配置会使应用程序崩溃。 我正在使用@ nuxt / auth-next:^ 5.0.0-1595976864.509d9d6

// nuxt.config.js
auth: {
    strategies: {
      cookie: {
        cookie: {
          name: 'session',},endpoints: {
            login: { url: '/api/login',method: 'post' },logout: { url: '/api/logout',method: 'post' }
        }
      }
    }
 },
,

我已经解决了我的问题。我遇到了nuxt-auth的以下问题 https://github.com/nuxt-community/auth-module/issues/286 我在同一台服务器上使用了身份验证服务和nuxt,这导致nuxt auth模块陷入无限循环(更多详细信息,请参见上面的链接)。 因此我为API服务和SSR(nuxt)创建了单独的服务器。

以下是我的nuxt-auth配置,没什么区别。

  auth: {
redirect: {
  login: "/register",home: "/",logout: "/register",user: "/user",callback: "/",strategies: {
  local: {
    endpoints: {
      login: {
        url: "/login",method: "post",propertyName: "token",logout: { url: "/logout",method: "post" },user: { url: "/user",method: "get",propertyName: "user" },tokenRequired: false,tokenType: false,

},

更重要的是,这是我的axios配置。请注意,我正在向其他服务器发出请求。

axios: {
baseURL: "http://localhost:5000/",credentials: true,init(axios) {
  axios.defaults.withCredentials = true;
},

},

现在,有些东西与原始问题无关, 还请确保,如果您是从页面中间件发出API请求,则发送了正确的Cookie。

查看此链接,https://github.com/nuxt/nuxt.js/issues/454 由于页面中间件可能在SSR的服务器端执行,因此cookie可能在那里不可用。有一些模块可以使cookie在服务器端可用。但是,相反,您可以仅将API请求放置在浏览器中执行的方法中。创建,获取等。

对不起,我急着写这个答案,待会我会改进。