Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static rewrite issue #283

Open
mindeka opened this issue Jul 4, 2013 · 12 comments
Open

Static rewrite issue #283

mindeka opened this issue Jul 4, 2013 · 12 comments

Comments

@mindeka
Copy link

mindeka commented Jul 4, 2013

Pretty sure this is due to my ineptitude, so I apologize in advance, but here goes:

IIS8 with rewrite module.
node Express app, hosted on default web site, under directory "/foo".
So the dir structure is "foo/app.js", "foo/public/css/styles.css" etc.
Accessed like "http://localhost/foo" or "http://pc-name/foo" over LAN.

Rewrite rules in web.config is setup as recommended:

<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="app.js" />
</rule>

Routes in app.js are set up like so:

app.get('/foo', render.index);
app.get(/foo/bar', render.bar);
// etc.

Express static handling turned off.
This resulted in 404 for any of the static files.
My solution, which worked, is to make a rule for each static file in web.config individually like so:

<rule name="style_css" stopProcessing="true">
    <match url="style.css" />
    <action type="Rewrite" url="public/css/style.css" />
</rule>

and set links in the template like so:

doctype 5
html
    head
        title= Foo
        link(rel='stylesheet', href='foo/public/css/style.css')

Which makes them load properly, but it seems hackish and is kind of a PITA to do it this way.
The question is, how to do it properly?

@tjanczuk
Copy link
Owner

tjanczuk commented Jul 9, 2013

URL rewriting configuration is a little bit like PERL programming: write only language. Did you check out the URL rewrite rules in the web.config template in http://tomasz.janczuk.org/2012/05/yaml-configuration-support-in-iisnode.html? They should cover your needs I think.

@mindeka
Copy link
Author

mindeka commented Jul 9, 2013

Thank you for reply. Yes, I actually did use the exact config from that post, with no luck :(

@ismyrnow
Copy link

I believe the issue is because {REQUEST_URI} includes the subdirectory that the node application is in ("foo" in this case). So the rewrite is turning "/css/style.css" into "public/foo/css/style.css", which is not what is expected.

The only workaround I know of besides not hosting your node app in a subdirectory is nesting your static files on level deeper in a folder named the same as your application. For example:

public
  foo
    css
    - style.css

This allows you to use the boilerplate static rewrite rule:

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

@alvarotrigo
Copy link

+1...

@alvarotrigo
Copy link

Is it open because actually there's no solution for it to server static files?

Or is it open just because the author didn't find a solution for his problem.

@aeciolevy
Copy link

@tjanczuk @rramachand21 I am running iisnode and node.js.
I could not access my public folder. I followed what you recommended on this post: #160 (comment)
However it didn't work.
I tried to use this in my server.js, but did not work either:

app.use(express.static(__dirname + "public"));
app.use(express.static("public"));

this is my route:

app.get("/usina2", function(req, res) {
  res.render("index.ejs");
});

This is my project structure:
screen shot 2017-05-08 at 3 23 48 pm

@thall1961
Copy link

I'm having the same problem as @aeciolevy , the only difference is that I handle the url rewriting in app.js that gets pulled into start.js. I tried nesting the public folder contents in a folder named the same as the app, but that didn't work. It's got to be a simple thing that I'm missing...

@rayterrill
Copy link

Bump. Running into this same exact issue.

@miljosh68
Copy link

Has this worked? I am having problems serving CSS and JS files. I have defined the following in server.js,
app.use("/assets", express.static(__dirname + "/css_js_files"));

The 'css_js_files' directory is in the same directory as my server.js. But, I I always get '404' error for the CSS or JS files that I try to load from the EJS file using <link href="assets/mystyles.css" rel="stylesheet" media="screen" />

I am using the following rule in my web.config.

<rule name="StaticContent" stopProcessing="true">
          <match url="\.(?:css|js)$" />
          <action type="Rewrite" url="assets{REQUEST_URI}" logRewrittenUrl="true" />
</rule>

Last but not the least, I have 'myapp' virtual directory created in IIS. So, my URL looks like 'http://localhost/myapp/index.html'
Thanks.

@cfrefah
Copy link

cfrefah commented Jan 9, 2019

Banged my head against the wall with this issue. I only wanted to serve static files in my root/styles- and root/scripts-folder. This finally solved it for me, and is independent of location of files:

                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="true" />
                </conditions>
                <match url="\.(?:jpg|jpg|css|png|js|ico|html|eot|woff|woff2|ttf|gif)$" />
                <action type="Rewrite" url="{REQUEST_URI}" logRewrittenUrl="true" />
            </rule>

@craigdrabiktxmq
Copy link

craigdrabiktxmq commented Aug 1, 2019

Unless it's important that Express handle your static files, why not let IIS handle it for you? Add a condition of conditions to exclude your static files from the rewrite rule and they will fall back to IIS serving your static content. Something like:

<rule name="DynamicContent">
  <conditions>
    <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True" />
    <add input="{REQUEST_URI}" negate="True" pattern="^/public" />
  </conditions>
  <action type="Rewrite" url="app.js" />
</rule>

@jasmasher
Copy link

jasmasher commented Nov 22, 2024

app.use(`/public`, express.static(`public`));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests