node.js - 将 Angular Universal 部署到 IIS

标签 node.js angular express angular-universal

我在 web.config 中将 Node.js 部署到 IIS 时遇到问题:

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

 <rule name="ng-universal" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{HTTP_HOST}" pattern="test" />
        </conditions>
        <action type="Rewrite" url="dist/server.js" />
    </rule>

我收到此错误:

image

这就是我的网络文件树的样子:

web files

我尝试授予 IIS_IUSRSIUSR 完全权限,但似乎不起作用。就像这篇文章建议的那样: iisnode encountered an error when processing the request

最佳答案

如果您想通过服务器端渲染运行 Angular 应用程序:

1-创建一个新项目:

ng new anguniversal

2-启用服务器端渲染(SSR)

ng add @nguniversal/express-engine

第 1 步

首先执行下面给出的命令,使用 Express 服务器构建 Angular 应用程序。
npm run build:ssr

现在您可以在项目文件夹中看到 dist 文件夹。

第 2 步

在 Windows 服务器中创建一个新文件夹(例如将其命名为 angular-universal),然后从此处的项目文件夹复制 dist 文件夹

第 3 步

从 dist/anguniversal/server 文件夹复制 main.js 文件并将其直接粘贴到 Anguniversal 文件夹中。

第 4 步

创建并打开 Web.config 文件(位于 Angular-universal 文件夹中)并向其中添加代码。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="main.js" verb="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="DynamicContent">
          <match url="/*" />
          <action type="Rewrite" url="main.js"/>
        </rule>
        <rule name="StaticContent" stopProcessing="true">
          <match url="([\S]+[.](jpg|jpeg|gif|css|png|js|ts|cscc|less|ico|html|map|svg))" />
          <action type="None" />
        </rule>
      </rules>
    </rewrite>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" />
      <remove fileExtension=".svg" />
      <remove fileExtension=".eot" />
      <remove fileExtension=".ttf" />
      <remove fileExtension=".woff" />
      <remove fileExtension=".woff2" />
      <remove fileExtension=".otf" />
      <mimeMap fileExtension=".ttf" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".svg" mimeType="image/svg+xml"  />
      <mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject" />
      <mimeMap fileExtension=".woff" mimeType="application/x-woff" />
      <mimeMap fileExtension=".woff2" mimeType="application/x-woff" />
      <mimeMap fileExtension=".otf" mimeType="application/otf" />
    </staticContent>
  </system.webServer>
</configuration>

第 5 步

安装IIS Node & URL Rewrite在服务器上。

第 6 步

在 IIS 中添加网站...。 enter image description here

第 7 步

在 IIS 中,转到“应用程序池”。对于您创建的网站( Angular 通用)。然后将 .NET CLR 版本更改为“无托管代码”。

enter image description here

完成,您可以查看结果。

关于node.js - 将 Angular Universal 部署到 IIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49024686/

相关文章:

javascript - 使用 multer 发送多个表单数据到express API

javascript - 如何在不重新加载页面的情况下更改 BrowserWindow 哈希 url

node.js - AWS 上 Node 应用程序的 Docker 退出状态 1

angular - 在 Angular 中,什么是 'pathmatch: full',它有什么作用?

angular - 如何在我的 Angular 应用程序中使用 Firebase Admin SDK?

javascript - 将查询参数附加到 GET 请求?

mysql - 共享服务器上的 Rest API

node.js - 每次测试前无法获取所需文件

angular - 如何在ionic 3中的div内设置背景图像?

javascript - 在新选项卡中渲染静态内容(express、ejs)