简体   繁体   English

无法在IIS中运行Angular2应用

[英]Unable to run the Angular2 app in IIS

I followed the Angular2 Quick start example and used live-server to run it. 我遵循了Angular2 快速入门示例,并使用实时服务器来运行它。 It works fine. 工作正常。

I created then a web site in IIS and set the virtual directory to the directory where I have the index.html file. 然后,我在IIS中创建了一个网站 ,并将虚拟目录设置为我拥有index.html文件的目录。 When I run the website I get the following error. 当我运行网站时,出现以下错误。 The error happens when the IIS tries to server app.ts. 当IIS尝试服务器app.ts时,会发生此错误。

How can run the Angular2 app in IIS? 如何在IIS中运行Angular2应用程序?

The page you are requesting cannot be served because of the extension configuration. 由于扩展程序配置的原因,无法提供您请求的页面。 If the page is a script, add a handler. 如果页面是脚本,请添加处理程序。 If the file should be downloaded, add a MIME map. 如果应下载文件,请添加MIME映射。

UPDATE: 更新:

Solution: I created a new web.config file and added the following snippet of code to add the MIME type. 解决方案:我创建了一个新的web.config文件,并添加了以下代码段以添加MIME类型。 This will be used by the IIS as the local web app settings. IIS将其用作本地Web应用程序设置。

<configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".ts" mimeType="application/x-typescript" />
        </staticContent>
    </system.webServer>
</configuration>

在此处输入图片说明

By convention, it is not advised to directly serve TypeScript (.ts) files. 按照惯例,不建议直接提供TypeScript(.ts)文件。 This is why IIS doesn't enable this by default. 这就是为什么IIS默认情况下不启用此功能的原因。 The quickstart tutorial also explicitely mentions this halfway the page, on section What's wrong with this? 快速入门教程还在页面的一半中明确提到了此问题是什么? , where it states the following: ,其中指出以下内容:

We were up and running in a hurry and we could explore Angular in this manner for quite some time. 我们匆忙起跑,可以在相当长的一段时间内以这种方式探索Angular。 For a number of reasons this isn't a good approach for building an application: 由于多种原因,这不是构建应用程序的好方法:

  • Transpiling TypeScript in the browser becomes tediously slow when our app grows beyond a few files. 当我们的应用程序扩展到几个文件之外时,在浏览器中转换TypeScript就会变得非常乏味。 We certainly won't do that in production. 我们当然不会在生产中这样做。 We should learn to compile locally and push the generated JavaScript to the server. 我们应该学习在本地编译并将生成的JavaScript推送到服务器。 We'll need some tools for that. 为此,我们需要一些工具。

So if you take a couple of minutes more and finish the tutorial, it'll work out for you just fine without having to bother with IIS configuration. 因此,如果您花几分钟时间完成本教程,那么它就可以很好地工作,而无需理会IIS配置。

That being said, if you truly want it, it is possible. 话虽如此,如果您真正想要它,那可能的。 Like mentioned in other comments, you can add a MIME type for .ts files in IIS. 就像其他注释中提到的那样,您可以在IIS中为.ts文件添加MIME类型。 In the SO question What's the MIME-Type of TypeScript it is explained as well. 在SO问题中,还解释了TypeScript的MIME类型

try to register a .ts mime type 尝试注册.ts MIME类型

Add a MIME Type (IIS 7) 添加MIME类型(IIS 7)

Also, this SO seems like a good explanation 另外, 这个SO似乎是一个很好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM