How to run Node.JS server for a web application?
Asked Answered
W

4

31

Info: I am very new to node.JS!

I have written a sample server that can listen to http requests on port XXXX. When I run this server from commandline (Windows) it seems to work well. It responds to the requests made to localhost:XXXX when opened in a browser.

Question: Is this how this is supposed to work? For the node server to run, should there always be a CMD prompt open for the server to listen to requests? Can I not do "something" with IISNode?

I understand that if I make a request to a JS files, which is noted in IISNode as a Node.JS file and that NODE should be handling it; then I will have Node handling the request for me. But then this assumes that IIS is the web server for me and that specific requests can be handled by Node.

I hope I am making sense here! :)

Weak answered 8/5, 2013 at 7:13 Comment(3)
Dunno about iisnode, but on linux/unix you want to run the process in the background using either nohup or forever. The latter one is great for keeping your application running even if it crashes.Insula
I am not familiar with Unix much. Do you mean a shell prompt is always open so that the node server keep on running? Or is it that it can be run in background, like a background-process, invisible to userWeak
Yes exactly the process is run in the background. I think you could look into forever, should work on windows machines too (but as I said previously, I have no idea if iisnode have such functionality built in).Insula
W
21

I solved it using a proper method. Yes, IISNode it is.. But none of comments seemed to answer how to "run" app.js for different applications hosted on same IIS (which is also serving PHP, ASPX, etc)

Step 1. Edit your node application’s entry-point (typically) app.js for the new URL structure.

An express app assumes that it owns the entire URL space and starts the URLs from the root itself, as shown:

Default EXPRESS App.js

Edit you app.js to look like the following (but put YOUR app’s directory name instead of “aaspass”!!):


app.js modified as per directory structure of app hosted on IIS


Now put a web.config file at the root of your app which looks like the following (You may use this template: webconfig).

Again edit the file and change the name “aaspass” to your app’s directory name.


Modified We.Config to add rules to redirect to relevant app.js

Thats it! You may do this for as many apps as required and host them on SAME server.

Weak answered 16/7, 2013 at 8:2 Comment(0)
E
24

On Windows you have two options of hosting node.js applications:

  1. Self-host the node.exe process just like you would on *nix. During development you will probably just start it from the command line. In production you want to come up with a mechanism that will provide process lifetime management around node.exe (e.g. start it when the OS starts). The most reasonable way of doing it on Windows is to use Windows Services (also known as NT Services). A component that can help you do this is http://nssm.cc/.
  2. Host node.js with the IIS using iisnode (http://github.com/tjanczuk/iisnode). Compared to self-hosting this method has a number of benefits outlined in https://github.com/tjanczuk/iisnode/wiki. But you also want to explore the performance implications (not all of them bad actually): http://tomasz.janczuk.org/2012/06/performance-of-hosting-nodejs.html.
Excoriate answered 8/5, 2013 at 18:51 Comment(4)
When we say "host using IISNode", what do we mean? Include the app.js using <script tags in the header? How do I HOST it using IISNode...??Weak
Is it possible to use nssm and get the user id of currently logged in user?Soho
If you "self-host" what about IIS using port 80 at the same time as node.js. I mean who hosts a site on port 3000 lolHarpist
@Harpist you can use IIS ARP to do a reverse proxy iis.net/learn/extensions/url-rewrite-module/…Eyecup
W
21

I solved it using a proper method. Yes, IISNode it is.. But none of comments seemed to answer how to "run" app.js for different applications hosted on same IIS (which is also serving PHP, ASPX, etc)

Step 1. Edit your node application’s entry-point (typically) app.js for the new URL structure.

An express app assumes that it owns the entire URL space and starts the URLs from the root itself, as shown:

Default EXPRESS App.js

Edit you app.js to look like the following (but put YOUR app’s directory name instead of “aaspass”!!):


app.js modified as per directory structure of app hosted on IIS


Now put a web.config file at the root of your app which looks like the following (You may use this template: webconfig).

Again edit the file and change the name “aaspass” to your app’s directory name.


Modified We.Config to add rules to redirect to relevant app.js

Thats it! You may do this for as many apps as required and host them on SAME server.

Weak answered 16/7, 2013 at 8:2 Comment(0)
S
8

What worked for me:

  1. Install IISNode
  2. Install URL Rewrite module of IIS
  3. Add web.config file in your Node.js app/folder. Here are the content of web.config file:

    In the handler, I just need to point to app.js (typical entry point of your application). I have not made changed to any of my routes (there is no need to append any text).

..

<configuration> 
        <appSettings>
            <add key="NODE_ENV" value="production" />
        </appSettings>
          <system.webServer>



    <handlers>
      <add name="iisnode" path="server/app.js" verb="*" modules="iisnode" />
    </handlers>

     <rewrite>
      <rules>
       <clear />
        <rule name="cdw">
          <match url="/*" />
          <action type="Rewrite" url="server/app.js" />
        </rule>
      </rules>
    </rewrite>

  </system.webServer>
</configuration>
Soho answered 23/7, 2015 at 8:59 Comment(0)
C
2

If you are on Windows you can (and probably should) run Node.js under IIS:

http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx

Corium answered 8/5, 2013 at 13:32 Comment(1)
I did read that page... It does not seem to answer my question though :(Weak

© 2022 - 2024 — McMap. All rights reserved.