【问题标题】:405 Method not allowed on express angular 5 app deployed using azure web app使用 azure web 应用程序部署的 express angular 5 应用程序上不允许使用 405 方法
【发布时间】:2018-02-14 05:06:59
【问题描述】:

我使用 azure web 应用程序部署了我的 angular + nodejs 应用程序,但是在尝试访问我的节点 api 时出现 405 method not allowed 错误

server.js 静态资产代码

/*API routes*/
const api = require("./server/routes/route");

/*Enable cors origin*/
var corsOptions = {
  "origin": "*",
  "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
  "preflightContinue": false,
  "optionsSuccessStatus": 204
}
app.use(cors(corsOptions));

app.use(express.static(path.join(__dirname, "/dist")));

app.use("/api", api);

app.get("/", function(req, res) {
  res.sendfile(path.join(__dirname, "/dist/index.html"));
});

因为我使用的是 iis,所以我在 dist 文件夹的根目录中添加了一个 web.config 作为这个要点

https://gist.github.com/iammelvin/62e2582796cbc3fa8b5fc9e242915788

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <system.webServer>

    <webSocket enabled="false" />

    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
    </handlers>

    <rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^server.js\/debug[\/]?" />
        </rule>

        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>

        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="server.js"/>
        </rule>
      </rules>
    </rewrite>

    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <httpErrors existingResponse="PassThrough" />

  </system.webServer>
</configuration>

我使用构建脚本构建项目

 "scripts": {
    "ng": "ng",
    "start": "node server.js",
    "build": "npm run clean && ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "dev": "concurrently  \"nodemon --inspect-brk=3000 server.js --watch server\" \"ng serve --proxy-config proxy.config.json\""
  },

我的 Angular cli json 看起来像这样

"apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": ["assets", "favicon.ico"],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app"...}]

最后我的索引基础href是&lt;base href="/"&gt;

【问题讨论】:

    标签: node.js azure azure-web-app-service angular5


    【解决方案1】:

    您需要确保将 web.config 文件放在应用的根目录中 (D:\home\site\wwwroot)。

    根据您的web.config,您的文件夹结构应如下所示:

    D:\home\site\wwwroot
    ├── node_modules 
    │   └── ...
    ├── public
    │   └── ...
    ├── server.js
    ├── package.json
    └── web.config
    

    实际上,您的 server.js 文件中不再需要这些行:

    // remove these lines of the code as well
    app.use(express.static(path.join(__dirname, "/dist")));
    
    app.get("/", function(req, res) {
      res.sendfile(path.join(__dirname, "/dist/index.html"));
    });
    

    现在,您可以使用此端点访问您的快速路由:

    https://{yourWebAppName}.azurewebsites.net/api/{routeName}

    并使用此端点检索静态文件:

    https://{yourWebAppName}.azurewebsites.net/{theFileInPublic}

    【讨论】:

    • 所以我必须通过 ftp 添加它们?如果我在 dist root 中添加 web.config,vs code 中的 azure app extension 不会这样做吗?在这种情况下,如果我的应用计划是基本的,我会从 azure 获取 ftp 详细信息吗?
    • 查看here获取 FTP 详细信息,或者您可以使用 Kudu cmd (https://.scm.azurewebsites.net/DebugConsole) 来操作您的应用程序的远程文件。跨度>
    • 我尝试在 wwwroot 文件夹中添加 web.config 但我收到此错误“找不到页面”我的 dist 文件夹中的所有文件也在 wwwroot 文件夹中。顺便说一句,我应该在其中添加参考 server.js src/index.html ?? src 是我有客户端应用程序文件的文件夹。
    • 不,你不应该。根据 web.config 中的这一行 &lt;action type="Rewrite" url="public{REQUEST_URI}"/&gt;,您需要将静态文件放入 /public 文件夹或将 src 重命名为 public
    • 所以我应该在 wwwroot 中创建一个名为 public 的文件夹并将文件放入其中??
    猜你喜欢
    • 2018-07-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多