Nodejs on IIS listen port and default file issues

493 Views Asked by At

So I have my server up and running.

I have node installed, iis, Application Request Routing etc.

I have a basic nodejs file calling express.

I have it listening on port 3000.

I have my domain pointing to my server ip, router port forwarding to server and everything is accessible from the outside.

Here is where I am confused.

im in sort of a catch 22 situation right now.

if I locate localhost it loads the default file app.js and display all the code as text in the browser window.

if I access localhost:3000 it loads the nodejs file perfectly.

if i try to access using www.mydomain.com it spits out plain text of all the code in my app.js file.

if I reverse proxy using rewrite url and point to port 3000 i get CANNOT GET /app.j

if I remove the {R:0} from the reverse proxy it all works for, except I cant access anything else on my server but localhost:3000

its my 3rd attempt at setting up a node server. i have tried linux, now im on windows iis and its always the same outcome. i end up in these works one way but not the other and can only access files on listen server localhost but not from main domain.

Anyone have any insight into what's going on?

TL:DR if i reverse proxy i can access my node listen server localhost:3000 but nothing else inside root folder since url rewrite bypasses everything else. so mydomain.com/index.htm shows CANNOT GET /index.htm. flipside-> not reverse proxying shows default files of app.js and displays it as plain text. but i can explore site via url ex mydomain.com/directory/index.htm

Is there a way I can have my default page be my app.js file running on listen port and i can still also link to other pages ex.(mydomain.com/directory/news.html)?

1

There are 1 best solutions below

1
Qiang Fu On

IIS cannot process nodejs file directly without installing nodejs handler. So you will see code text if you set app.js as default page.
Using reverse proxy is a general solution to host a node.js webapp. You can exclude html files in the URL Rewriting rule.
The following web.config will let you get .htm or .html when making nodejs webapp as domain.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="^((?!\.htm).)*$" />
                    <action type="Rewrite" url="http://localhost:3000/{R:0}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>