Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 快速中间件被CORS策略阻止(飞行前选项请求出现授权错误)_Node.js_Express_Cors_Authorization_Preflight - Fatal编程技术网

Node.js 快速中间件被CORS策略阻止(飞行前选项请求出现授权错误)

Node.js 快速中间件被CORS策略阻止(飞行前选项请求出现授权错误),node.js,express,cors,authorization,preflight,Node.js,Express,Cors,Authorization,Preflight,当这个块不存在时,我的API工作得很好 添加此块时,会出现CORS错误: 对飞行前请求的响应未通过访问控制检查:它没有HTTP ok状态 我错过了什么 app.use((req, res, next) => { try { if (secret !== config.secret) { throw new Error('Not Authorized') } else { next() } } catch (err) { nex

当这个块不存在时,我的API工作得很好

添加此块时,会出现CORS错误:

对飞行前请求的响应未通过访问控制检查:它没有HTTP ok状态

我错过了什么

app.use((req, res, next) => {
  try {
    if (secret !== config.secret) {
      throw new Error('Not Authorized')
    } else {
      next()
    }
  } catch (err) {
    next(err)
  }
});

如果请求的方法为OPTIONS,请确保未引发错误:

if (req.method !== "OPTIONS" && secret !== config.secret) {
  throw ...
}

请参阅删除CORS错误的。

。非常感谢。