Search code examples
node.jsreactjsazurevisual-studio-codecreate-react-app

Why would my NodeJS app return a static Javascript file when I hit its root URL


I installed my React app on Azure App Services using the App Services extension in VS Code. IT said it has successfully installed. I had some issues and had to set a default document of index.js in the web.config to avoid a well-known issue with NodeJs and App Services. However, when I hit the main url of my app, I get back the index.js served back to me a static file. It does not execute the file and render my app, as I expect it to. I have checked via my Kudu's debug console and the Node and npm are installed. The correct versions of them, in fact. I have sat with this issue for 2 whole days and cannot get the app to run.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { mainReducer } from './main/reducer';
import * as AuthService from '../src/authentication/authentication-service';

let store = createStore(mainReducer, {});

AuthService.init();

require('dotenv').config();

AuthService.runAuth(() => {
  const Bootstrap = () => (
    <Provider store={store}>
      <App />
    </Provider>
  );

  ReactDOM.render(<Bootstrap />, document.getElementById('root'));
  serviceWorker.register();
});

My web.config file looks like:

<?xml version="1.0" encoding="utf-8"?>
<!--
     This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:
     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->

<configuration>
  <system.webServer>
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <webSocket enabled="false" />
    <handlers>
      <!-- Indicates that the index.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
    </handlers> 
    <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^index.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 site entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="index.js"/>
        </rule>
      </rules>
    </rewrite>

    <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <!-- Make sure error responses are left untouched -->
    <httpErrors existingResponse="PassThrough" />

    <!--
      You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled
      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
    -->
    <iisnode watchedFiles="web.config;*.js" debuggingEnabled="true"/>
  </system.webServer>
</configuration>

Solution

  • You should configure your app to send the index.html rather than the index.js

    check 1

    Did you deploy the correct build folder ? (never used the Azure App Services but from my understanding it should be the bundle version)