22

We created a Linux Web App in Microsoft Azure. The application is static written with React (html and Javascript). We copied the code into the wwwroot folder, but the application only showing only hostingstart.html and when we try to get page index.html we have this error: Cannot GET /index.html

We tried with a sample of Azure in GitHub (https://github.com/Azure-Samples/html-docs-hello-world) but the error is the same. The url is this: https://consoleadmin.azurewebsites.net/index.html

Last week the application was running correctly.

We forget to do something?

4 Answers4

59

MAY 2020 - You don't have to add any javascript files or config files anywhere. Let me explain.

I was facing this exact same issue and wasted 6 hours trying everything including the most popular answer to this question. While the accepted answer is a nice workaround (but requires more work than just adding the index.js file), there's something a simpler than that.

You see, when you just deploy an Azure Web App (or App Service as it is also called), two things happen:

  1. The web app by default points to opt/startup/hostingstart.html

  2. It also puts a hostingstart.html in home/site/wwwroot

When you deploy your code, it replaces hostingstart.html in home/site/wwwroot but the app is still pointing to opt/startup/hostingstart.html. If you want to verify this, try deleting opt/startup/hostingstart.html file and your web app will throw a "CANNOT GET/" error.

So how to change the default pointer? It's simpler than it looks:

Go to Configuration tab on your web app and add the following code to startup script:

pm2 serve /home/site/wwwroot --no-daemon

If this web app is a client-side single-page-app and you're having issues with routing, then add --spa to the above command as follows:

pm2 serve /home/site/wwwroot --no-daemon --spa

This will tell the web app to serve wwwroot folder. And that's it.

Image for reference: Screenshot explaination

PS: If you only set the startup script without deploying your code, it will still show the hostingstart.html because by default that file lies in the wwwroot folder.

DeityWarrior
  • 716
  • 1
  • 6
  • 5
  • 2
    This approach was helpful, but I could not get pm2 to serve all my generated directories and files with gatsby. Gatsby produces static files and folders with react SSR. When using `pm2 serve /home/site/wwwroot`, the SPA runs fine, but when navigation (within the app (not and SPA)) to /dir/dir and then hitting back button or refreshing, I get an "Sorry, check with the site admin for error: EISDIR .." error, so it will not serve anyting other than index.html.. – phun-ky Jun 10 '20 at 12:48
  • If I target /dir/dir/index.html directly, that works, and I am back into the "ecosystem" of the app – phun-ky Jun 10 '20 at 12:56
  • 1
    Ugh, I'm so close to solving this! I'm running .NET Core / ASP.NET Core, so a Node answer doesn't help. But at least I know the problem! – Grault Aug 29 '20 at 04:12
  • @phun-ky try adding the --spa in the end of the startup command. I've updated my answer. I faced this issue as well. – DeityWarrior Sep 16 '20 at 13:00
  • it is working for me: `pm2 serve /home/site/wwwroot --no-daemon --spa` – beewest Jan 09 '21 at 15:35
  • In my case I have to select older version of Node (Node 10) – Lopuch Jan 28 '21 at 14:59
  • @phun-ky Have you resolved your issue related to gatsby? I am facing this issue and unable to resolve it. – TechGuru Feb 26 '21 at 15:01
  • If I could dry-hump this answer, i would. Thank you! worked perfectly. Did not want to change source code due to the environment the site is deployed to. – Darren Wainwright Mar 08 '21 at 23:33
  • @TechGuru Yes I have, we switched to the php stack (not the code, but the azure image (?) ) and we are using the built in apache server to serve the static files – phun-ky Mar 16 '21 at 12:04
  • 2
    I also solved this by changing to PHP stack with empty startup command. – Ian Lin Mar 19 '21 at 10:27
  • Worked perfectly, thank you!! This should probably be documented somewhere as well, as it has happened several times when deploying to new linux containers on azure app services. Also, like @Grault, I could also do with a .NET Core fix for this as well if anyone knows one, as I have another app that runs with a web.config file. Same issue happens, just see the default hostingstart.html no matter what, and this command does not work on that stack. – Jorje Redemption Sep 09 '22 at 11:13
  • OMG where in the documentation is this? – Christian Matthew Oct 17 '22 at 13:07
17

Ok you are gonna love this. This happened to me today also. Same exact thing. I am pretty sure the azure team flipped a switch somewhere and we fell through a crack.

I found this obscure answer with no votes and it did the trick (with a little extra finagling)

BONUS! this also fixed my router issues I was having only on the deployed site (not local):

Credit: @stormwild: Default documents not serving on node web app hosted on Azure

From @stormwild's post see here: https://blogs.msdn.microsoft.com/waws/2017/09/08/things-you-should-know-web-apps-and-linux/#NodeHome

Steps:

  1. Go to your azure portal, select your app service and launch ssh
  2. List item
  3. In ssh terminal, navigate via command line to /home/site/wwwroot
  4. create index.js there with the following code:

    var express = require('express');
    
    var server = express();
    
    var options = {
    
    index: 'index.html'
    
    };
    
    server.use('/', express.static('/home/site/wwwroot', options));
    
    server.listen(process.env.PORT);
    
  5. NOTE: Be sure to run npm install --save express also in this folder else your app service will crash on startup

  6. Be sure to restart your app service if it doesn't do so automagically

Karthikeyan VK
  • 5,310
  • 3
  • 37
  • 50
Richard Strickland
  • 1,771
  • 17
  • 8
  • 1
    thanks! I solved but i needed to add the **port** in the index.js, server.listen(process.env.PORT);. I also did 'npm init' at the beginning and 'npm start' after 'npm install --save express' – invisibleProgrammer Jan 18 '19 at 09:21
4

A workaround, I changed the webapp stack to PHP 7

enter image description here

Rodolfo Luna
  • 829
  • 9
  • 19
2

Another solution would be to add a file called ecoysystem.config.js right next to your index.html file.

module.exports = {
  apps: [
    {
      script: "npx serve -s"
    }
  ]
};

This will tell pm2 to associate all requests to index.html as your app service starts up.

Very helpful information here: https://burkeholland.github.io/posts/static-site-azure/

Per
  • 491
  • 6
  • 19