【问题标题】:Node.js & Express web API not working on azureNode.js 和 Express Web API 无法在 azure 上运行
【发布时间】:2022-09-27 10:03:58
【问题描述】:

我使用这个示例创建了一个带有 React SPA 和 Node.js & Express Web API 的基本应用程序 https://github.com/Azure-Samples/ms-identity-javascript-react-tutorial/tree/main/5-AccessControl/1-call-api-roles

该应用程序在本地运行良好 - 前端身份验证和后端 API 调用都按预期工作。

当我在 Azure 上部署应用程序时,前端工作正常,但后端 API 调用根本不工作并且它们超时 - 我在后端也看不到任何日志。在本地,我可以看到调用 API 调用的所有 console.logs。

我已经尝试了许多更改来解决下面提到的 Node.js 应用程序问题,并且我正在寻求有关哪些其他设置/更改可能有助于让 Node.js 应用程序在 Azure 上运行的指导。

  • 我尝试在 Azure 以及 /wwwroot/server - node app.js 中手动运行后端,我看到日志显示 API 正在侦听,但 API 调用超时。

  • web.config.js 指向server/app.js

  • 在 Azure 上,React 应用程序仅在 port 443 上成功运行,如下所示,并且在设置为其他一些 PORT 时根本不运行:

client/package.json

...
\"scripts\": {
    \"start\": \"set PORT=443&& react-scripts start\",
...

当指定如下时,它在本地运行在所有端口上:

client/package.json

...
\"scripts\": {
    \"start\": \"set HTTPS=true&&set SSL_CRT_FILE=certificate.crt&&set SSL_KEY_FILE=privateKey.key&&set PORT=3000&& react-scripts start\",
...
  • 在 Azure 上获取调用超时
client/fetch.js


export const getTest = async (env) => {
    // const accessToken = await getToken();

    const headers = new Headers();
    // const bearer = `Bearer ${accessToken}`;

    // headers.append(\"Authorization\", bearer);

    const options = {
        method: \"GET\",
        headers: headers,
    };

    return fetch(protectedResources.apiList.test, options)
        .then(response => response.json())
        .catch(error => console.log(error));
}
  • 在本地 server/app.js, 中,注释掉和使用身份验证逻辑都适用于 API 调用,但在 Azure 上都不起作用。
  • 还尝试在 http 和 https 上运行 Node.js。 PS。本地只有 HTTPS 适用于客户端和节点,因为 redirectUri 是 https
server/app.js


const path = require(\'path\');
const express = require(\"express\");
const cors = require(\"cors\");
const axios = require(\"axios\");
const PORT = process.env.PORT || 8080;
require(\"dotenv\").config();
const app = express();
app.use(cors());
app.use(express.json());

//app.use(express.static(path.resolve(__dirname, \'../client/build\')));

app.get(\"/api/test\", (req, res) => {
  res.json(\"hi\");
});

//...other bearer token code

// https.createServer(httpsOptions, app).listen(PORT, () => console.log(`Server is running on port ${PORT}`));

app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));

    标签: node.js reactjs azure express single-page-application


    【解决方案1】:

    如果您使用的是 azure VPS,请从 azure 用户面板启用端口。由于 azure 限制了甚至从 Windows 防火墙启用的端口。因此,请确保从用户面板/控制面板启用端口。

    【讨论】:

      猜你喜欢
      • 2014-01-07
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多