locked
[Node.js] Having issue with deploying modern node.js app RRS feed

  • Question

  • Hi there, I have a few things I'm confused by/unclear on:

    What is the latest version of node.js I can tell an Azure Web App to use?

    Can I use babel-transform at runtime or do I need to pre-compile my assets?

    If I'm deploying from git, how do I get secret configuration keys in at app start without checking a secrets file into my repo?

    do I have to use "server.js" in the root of my repo as my starting file or can I start from "./dist/server.js" and how do I set that?

    how do I/can I tell Azure to run a build script then use "npm start" or some other command from package.json on deploy?

    Once i've gotten a basic deployment going, I'd like to introduce PM2 - process monitoring. PM2 has an option for "number of processes" which creates a process for each available CPU. In the context of Azure do I set that to one and manage "number of instances" through the admin portal?

    I've written my app in koa.js (an express-like framework) using the modern JS syntax (import, let/const, spread operators, async/await) supported by the 7.x line of node natively. I can also run it successfully in 6.x using the babel-transform plugin.

    I'm obviously fairly confused on some basics on deploying to Azure in the context of a node.js app using the modern bits. Any help would be appreciated.


    Wednesday, March 8, 2017 3:56 PM

Answers

  • What is the latest version of node.js I can tell an Azure Web App to use?

    You login the Kudu console site of your application, at the URL of https://<your_web_app_name>.scm.azurewebsites.net/DebugConsole, and change directory to D:\Program Files (x86)\nodejs>, you can find all the available nodejs version list on Azure Web Apps.


    Can I use babel-transform at runtime or do I need to pre-compile my assets?

    We can leverage Custom Deployment Script to pre-compile during the deployment task via Git. Here is example repos on GitHub, which leverages gulp and custom deployment script to compile the node.js from ES6 to ES5 via the Azure deployment task.

    And additionally, you can try to store your configuration keys in Application setting in Azure Web Apps, and then we can use the configurations in the custom deployment script, please refer to Customizing site deployment based on site's app settings in Azure Web Apps (Websites) for detailes.

    Windows Azure Websites uses IISNode to host the Node process inside of IIS. Your Node site is actually given a Named Pipe which receives the incoming requests, not a TCP port like you would use when running locally or hosting yourself. Even if you could open a TCP port, Azure Websites are really only meant for traditional websites and don't have a way to open ports to the outside world.

    So, first, you need to change the port in your code, e.g.:  var port = process.env.PORT||3000; //which you can run both on Azure or local var server = app.listen(process.env.PORT||3000);

    As a node.js application running on Azure Web Apps, needs a server.js or app.js file as the entrance in the root directory with a web.config file to control the iis (it will automatically create if you deploy via git or build the app server via node.js template). E.G.

    <configuration>
    <system.webServer>
    
        <handlers>
            <!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
            <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
        </handlers>
    
        <rewrite>
            <rules>
                <!-- Don't interfere with requests for logs -->
                <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
                </rule>
    
                <!-- Don't interfere with requests for node-inspector debugging -->
                <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                    <match url="^server.js\/debug[\/]?" />
                </rule>
    
                <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
                <rule name="StaticContent">
                    <action type="Rewrite" url="public{REQUEST_URI}" />
                </rule>
    
                <!-- 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="server.js" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>


    • Marked as answer by mraumer Thursday, March 9, 2017 1:44 PM
    Thursday, March 9, 2017 1:30 AM

All replies

  • What is the latest version of node.js I can tell an Azure Web App to use?

    You login the Kudu console site of your application, at the URL of https://<your_web_app_name>.scm.azurewebsites.net/DebugConsole, and change directory to D:\Program Files (x86)\nodejs>, you can find all the available nodejs version list on Azure Web Apps.


    Can I use babel-transform at runtime or do I need to pre-compile my assets?

    We can leverage Custom Deployment Script to pre-compile during the deployment task via Git. Here is example repos on GitHub, which leverages gulp and custom deployment script to compile the node.js from ES6 to ES5 via the Azure deployment task.

    And additionally, you can try to store your configuration keys in Application setting in Azure Web Apps, and then we can use the configurations in the custom deployment script, please refer to Customizing site deployment based on site's app settings in Azure Web Apps (Websites) for detailes.

    Windows Azure Websites uses IISNode to host the Node process inside of IIS. Your Node site is actually given a Named Pipe which receives the incoming requests, not a TCP port like you would use when running locally or hosting yourself. Even if you could open a TCP port, Azure Websites are really only meant for traditional websites and don't have a way to open ports to the outside world.

    So, first, you need to change the port in your code, e.g.:  var port = process.env.PORT||3000; //which you can run both on Azure or local var server = app.listen(process.env.PORT||3000);

    As a node.js application running on Azure Web Apps, needs a server.js or app.js file as the entrance in the root directory with a web.config file to control the iis (it will automatically create if you deploy via git or build the app server via node.js template). E.G.

    <configuration>
    <system.webServer>
    
        <handlers>
            <!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
            <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
        </handlers>
    
        <rewrite>
            <rules>
                <!-- Don't interfere with requests for logs -->
                <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
                </rule>
    
                <!-- Don't interfere with requests for node-inspector debugging -->
                <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                    <match url="^server.js\/debug[\/]?" />
                </rule>
    
                <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
                <rule name="StaticContent">
                    <action type="Rewrite" url="public{REQUEST_URI}" />
                </rule>
    
                <!-- 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="server.js" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>


    • Marked as answer by mraumer Thursday, March 9, 2017 1:44 PM
    Thursday, March 9, 2017 1:30 AM
  • this is all VERY useful information. thank you very much!

    Thursday, March 9, 2017 1:45 PM