使用URL Rewrite从iisnode提供静态文件 [英] Serving static files from iisnode with URL Rewrite

查看:127
本文介绍了使用URL Rewrite从iisnode提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在web.config文件中使用了一个重写规则,用于在issnode下运行的节点应用程序指向我的server.js文件。 myapp / *指向server.js。

I am using a Rewrite rule in my web.config file for a node app running under issnode to point to my server.js file. myapp/* points to server.js.

    <rule name="myapp" enabled="true">
      <match url="myapp/*" />
      <action type="Rewrite" url="server.js" />
    </rule>

这一直很好www.mywebsite.com/myapp/会加载运行我的应用程序。我想要的是从网站的根目录进行重定向,以便www.mywebsite.com/运行我的应用程序。所以我改变了我的web.config文件

This has been working great www.mywebsite.com/myapp/ would load a run my app. What I wanted was to have a redirect from the root of the website so www.mywebsite.com/ would run my app. So I changed my web.config file

    <rule name="myapp" enabled="true">
      <match url="/*" />
      <action type="Rewrite" url="server.js" />
    </rule>

所以这是运行server.js并提供我的静态html文件,唯一的问题从我的html文件(css,js,图像等)引用任何外部文件只需为每个请求获得500。我用它来提供静态文件

So this is running the server.js and serving up a my a static html file, the only problem is referencing any external files from my html file (css, js, images etc) Just get 500s for every request. I am using this to serve static files

        var libpath = require('path');
        var _path = "."; <-- This seems to be the problem


        var uri = url.parse(req.url).pathname;
    var filename = libpath.join(_path, uri);
    fs.readFile(filename, "binary", function (err, file) {
            if (err) {
            res.writeHead(500, {
                "Content-Type": "text/plain"
            });
                 res.write(err + "\n");
                 res.end();
                return;
            }

         var type = mime.lookup(filename);
            res.writeHead(200, {
                "Content-Type": type
            });
         res.write(file, "binary");
         res.end();
        });

    break;

所以我的问题是如何指向我的节点app / server的root来提供静态文件。

So my question is how to point to root of my node app / server to serve up static files.

谢谢

Jono

推荐答案

在iisnode中提供静态内容的最佳方法是配置URL重写模块,使IIS静态文件处理程序处理静态内容而不是node.js的请求。让IIS提供静态内容比使用任何node.js机制提供这些文件具有很大的性能优势,因为内核级别优化了缓存,而且不必破解JavaScript代码。

The best way to serve static content in iisnode is to configure the URL rewriting module such that the IIS static file handler handles requests for static content rather than node.js. Having IIS serve static content has a large performance benefit over serving these files using any node.js mechanisms due to kernel level optimizations around caching and just not having to break into JavaScript code.

对于实现此目的的样板文件web.config配置,请参阅 https://github.com/tjanczuk/iisnode/issues/160#issuecomment-5606547

For a boilerplate web.config configuration that achieves this see https://github.com/tjanczuk/iisnode/issues/160#issuecomment-5606547

这篇关于使用URL Rewrite从iisnode提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆