简体   繁体   中英

Unable to run the Angular2 app in IIS

I followed the Angular2 Quick start example and used live-server to run it. 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. When I run the website I get the following error. The error happens when the IIS tries to server app.ts.

How can run the Angular2 app in IIS?

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.

UPDATE:

Solution: I created a new web.config file and added the following snippet of code to add the MIME type. This will be used by the IIS as the local web app settings.

<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. This is why IIS doesn't enable this by default. 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. 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. We certainly won't do that in production. We should learn to compile locally and push the generated JavaScript to the server. 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.

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. In the SO question What's the MIME-Type of TypeScript it is explained as well.

try to register a .ts mime type

Add a MIME Type (IIS 7)

Also, this SO seems like a good explanation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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