应用程序中具有“模块”类型的 CORS"/>
NodeJS 应用程序中具有“模块”类型的 CORS
我在 VSCode 中有两个应用程序,一个 Python Flask 应用程序,它向 PayPal NodeJS 应用程序提交 POST 请求。 这是 NodeJS 应用程序上的PayPal 文档。
当我访问 http://127.0.0.1:5000 时,Flask 网络应用程序向位于 http://127.0.0.1:8888 的 PayPal NodeJS 应用程序提交一个 POST 请求。 Web 浏览器控制台捕获以下内容。
Cross-Origin Read Blocking (CORB) blocked cross-origin response with MIME type application/json. See for more details.
POST http://127.0.0.1:8888/my-server/create-paypal-order net::ERR_CONNECTION_REFUSED
这篇 Stack Overflow 帖子 有一个答案提到将 CORS 添加到 NodeJS。
PayPal NodeJS 应用程序是
"type": "module"
(不是"type": "commonjs"
)。我在 Google 和 Stack Overflow 上找到的大多数文章都解释了如何在 CommonJS NodeJS 应用程序中使用 CORS。我使用 npm install cors
在 VSCode 中安装了 CORS,然后将这些行添加到 NodeJS server.js.
const cors = import("cors");
app.use(cors());
我得到以下信息。
TypeError: cors is not a function
我不确定如何配置具有
"type": "module"
的 NodeJS 应用程序,以便 POST 请求不会被 CORS 策略阻止。
回答如下:
在类型模块中使用 cors 包的正确导入语法是 import cors from 'cors';
更多推荐
NodeJS 应用程序中具有“模块”类型的 CORS
发布评论