Eddy
Eddy

Reputation: 3713

node.js on Azure - You do not have permission to view this directory or page

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?

Upvotes: 8

Views: 11356

Answers (7)

Taras Gleb
Taras Gleb

Reputation: 187

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

Upvotes: 1

zenomat
zenomat

Reputation: 1

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]

Upvotes: 0

rohitrana043
rohitrana043

Reputation: 11

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.

Upvotes: 1

Indika Pradeep
Indika Pradeep

Reputation: 91

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

Upvotes: 0

Fernandoshell
Fernandoshell

Reputation: 51

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.

Upvotes: 5

Biswarup Ghoshal
Biswarup Ghoshal

Reputation: 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.

Upvotes: -1

Jay Gong
Jay Gong

Reputation: 23792

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.

Upvotes: 8

Related Questions