1

I have a node js script running Socket.io implementation in my web role. The only way i could find on how to start the node js script was running it in command prompt, so i created a background task in web role which runs the script in command prompt.

I set the property of "Copy To Output Directory" to "Copy If newer" for the node js file, cmd.exe and the node modules files and they are being generated in the bin folder. if i manually start the cmd.exe in the bin folder and run the command "node App.js", it works, the port on which the socket.io is listening is running. But when i run the command programatically like below it doesnt work.

It doesnt throw any error so i dont know if its a problem in my code or we cant start the node js script like this. Is there any way to find out if the command was executed properly or not? because no cmd prompt window was opened when the process starts. Any guidance, suggestions or ideas are deeply appreciated. Thanks

public class BackgroundThread
{
    public static void Start()
    {
        var appRoot = Path.Combine(Environment.GetEnvironmentVariable("RoleRoot") + @"\", @"approot");

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.CreateNoWindow = false;
        psi.FileName = Path.Combine(appRoot, "bin", "cmd.exe");
        psi.Arguments = "node app.js";
        psi.UseShellExecute = false;
        psi.WorkingDirectory = Path.Combine(appRoot, "bin");


        try
        {
            Process p = Process.Start(psi);

        }
        catch (Win32Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.WriteLine(ex.StackTrace);
            Console.WriteLine(ex.Source);
            Console.WriteLine(ex.ErrorCode);
        }       
    }

}

EDIT:

Turned out iisnode can handle everything , i just had to put a rewrite code in web.config. From a sample azure node js application from here http://www.windowsazure.com/en-us/develop/nodejs/tutorials/getting-started/ i got this rewrite code

<rewrite>
  <rules>
    <clear />
    <rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
      <match url="iisnode.+" negate="true" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
      <action type="Rewrite" url="App.js" />
    </rule>
  </rules>
</rewrite>

Its starting the App.js automatically, but its re directing all my views to the App.js script. I guess it has something to do with the re write code i put above. What changes should i make to get the App.js running on start up and still be able to access my other html views?

Thanks

Bitsian
  • 2,238
  • 5
  • 37
  • 72
  • It is standard for app.js to be lowercase, is this how your file is saved as well? – Four_lo Mar 12 '13 at 13:12
  • ohh is it? i have saved it as App.js. I will change that and try – Bitsian Mar 12 '13 at 13:28
  • sorry that is all that stands out, http://stackoverflow.com/questions/8173232/make-exe-from-node-js-app the second and third solution here may help, but any further suggestions would be a guess – Four_lo Mar 12 '13 at 13:47

2 Answers2

0

The best way to run node.js in a web role is to use iisnode - this is an iis http module that redirects iis requests to a node.exe process, allowing you to use iis process management with node.js apps.

You can do this easily using windows azure powershell, or azure websites. See http://www.windowsazure.com/en-us/develop/nodejs/tutorials/getting-started/ for details

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
0

One way to do this would be to put all static content in a particular folder and add a rewrite rule to bypass the redirection to app.js

`

                <!-- All other URLs are mapped to the Node.js application entry point -->
                <rule name="DynamicContent">
                     <conditions>
                          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                     </conditions>
                     <action type="Rewrite" url="app.js"/>
                </rule>
`