admin管理员组

文章数量:1187337

In a dev client/server deployment, my client is unable to request anything from the server, failing with a 401 unauthorized. The prod deployment is configured the same, and works just fine, so I wonder if this points to a server configuration issue, but regardless:

The issue is that, since this dev server deployment goes to a non-static base URL, I use a regex pattern match to check the URL, like so:

const app = express();

// cors
const corsOptions: CorsOptions = {
  origin: (origin, callback) => {
    const valid =
      !origin ||
      (CLIENT_URL
        ? CLIENT_URL === origin
        : new RegExp(CLIENT_URL_PATTERN).test(origin));
    callback(
      valid
        ? null
        : new Error(
            `Unsupported cross-origin request from ${origin} using pattern ${CLIENT_URL_PATTERN}`,
          ),
      valid,
    );
  },
  allowedHeaders: [
    'X-CSRF-Token',
    'X-Requested-With',
    'Accept',
    'Accept-Version',
    'Content-Length',
    'Content - MD5',
    'Content - Type',
    'Date',
    'X - Api - Version',
  ],
  methods: ['GET', 'OPTIONS', 'PATCH', 'DELETE', 'POST', 'PUT'],
  credentials: true,
};
app.options('*', cors(corsOptions));
app.use(cors(corsOptions));

Even with app.options('*', cors()) it still fails. Every request fails on preflight and they don't even pass through the origin callback for me to log anything. I'm stumped, and need some direction on where to look next.

本文标签: typescriptexpress preflight OPTIONS request fails with 401Stack Overflow