7

I created an App Service on Azure to deploy a Node.js app (express). I followed the doc to do a zip deploy with the tutorial app and it worked well.

Now I need to deploy my own app. I updated the web.config file with my own app's details -- app.js instead of index.js -- packaged it and uploaded. The upload process reported all went well.

But when hitting the url I get the following error: You do not have permission to view this directory or page.

What's next? How do I debug this?

Eddy
  • 3,533
  • 13
  • 59
  • 89

7 Answers7

6

What's next?

I crashed into the same error as you. You could refer to my working steps.

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.set('port', process.env.PORT || 5000);

console.log("+++++++++++++++"+ app.get('port'));

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

app.listen(app.get('port'), function(){
          console.log('Express server listening on port ' + app.get('port'));
          });

web.config

<?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 server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="app.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="^app.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="app.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"/>
  </system.webServer>
</configuration>

routes/index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

Access Result:

enter image description here

More details ,please refer to this case: Running Node.js on Azure Web App.

How to debug this?

To enable debugging, please follow the steps:

1) Create file iisnode.yml in your root folder (D:\home\site\wwwroot) if not exists.

2) Add the following lines to it.

loggingEnabled: true

logDirectory: iisnode

After that done, you can find logs in D:\home\site\wwwroot\iisnode.

Any concern, please let me know.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • Two questions. One, I'm using `winston` for debugging my app. Do I need to change it to your debugger? (I would absolutely prefer to avoid it, my code is already written.) Second question, how do I get to the App Service machine? From cloud shell? Any other way? – Eddy Nov 06 '18 at 14:11
  • @Eddy For One: You still could debug your app with your way,my debug way just let you know the specific error details. For Two: web app service is just P-A-A-S service,you can't access service machine directly. However , you could navigate to KUDU url to check your project. The url is: `https://.scm.azurewebsites.net/` – Jay Gong Nov 07 '18 at 01:52
  • @Eddy Maybe you could refer to this case:https://stackoverflow.com/questions/48390529/azure-default-logs/48393168#48393168 – Jay Gong Nov 07 '18 at 01:52
  • So what is the solution? – oded bartov May 24 '21 at 09:40
3

This problem is generally caused by the lack of web.config.

Solution that that worked for me:

the easiest Deploy the webapp in the Linux environment.

The easiest way is to use git for deployment. In git deployment, git will automatically create a web.config file.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30131390) – Mark S. Oct 20 '21 at 19:53
  • Adding web.config to root directory of the nodejs application solved the issue. To fix 1. Paste the content of https://github.com/projectkudu/kudu/wiki/Using-a-custom-web.config-for-Node-apps into your web.config 2. Replace 'server.js' with your application js file. e.g. index.js or app.js Redeploy application – Taras Gleb Sep 22 '22 at 02:10
1

I also faced this issue, this is because of the missing web.config file. Make sure to add application settings SCM_DO_BUILD_DURING_DEPLOYMENT by doing Application Settings>SCM_DO_BUILD_DURING_DEPLOYMENT>true For reference follow the MS Quickstart for node.

0

I've also had this issue when deploying Node.js API in Azure app service.

Add SCM_DO_BUILD_DURING_DEPLOYMENT key in web app > settings > configuration and set value to True

0

Another thing you can do is deploy manually and then check how the web.config looks like on Azure. I had the same issue and finally tracked it down to: first a missing web.config, then a bad web.config. I found the issue after manual deployment and checking the 2 config files - the correct one from Azure, the bad one that I was using.

You can use this command for manual deployment using Azure CLI:

az webapp up --sku F1 --name [AppServiceName] --os-type Windows --resource-group [ResourceGroupName]

zenomat
  • 1
  • 1
0

Adding web.config to root directory of the nodejs application solved the issue. To fix

  1. Paste the content of https://github.com/projectkudu/kudu/wiki/Using-a-custom-web.config-for-Node-apps into your web.config
  2. Replace 'server.js' with your application js file. e.g. index.js or app.js Redeploy application
Taras Gleb
  • 158
  • 2
  • 5
-1

The answer to this question lies on the package.json file - add this line in the package.json

"scripts": {
    "start": "node app.js"
},

And then deploy your app.

Anatoly
  • 20,799
  • 3
  • 28
  • 42