markzzz
markzzz

Reputation: 47947

What's wrong on deploy this nodejs app on Azure?

I've followed this tutorial of deploying nodejs app with VSCode, for Windows: https://learn.microsoft.com/en-us/azure/app-service/quickstart-nodejs?tabs=windows&pivots=development-environment-vscode

not sure why, but it "load" the whole directory on my app service, even if using that SCM_DO_BUILD_DURING_DEPLOYMENT param:

enter image description here

Here's the final remote folder (notice also the .vscode folder):

enter image description here

But of course, it does nothing loading the index (which should basically print "welcome", from index.js):

app.get('/', (req, res) => {
    res.send('welcome!')
})

It just says You do not have permission to view this directory or page.

Here's my package.json:

{
    "name": "@myapp/backend",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "supervisor index.js",
        "dev": "nodemon ./bin/www"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "bcryptjs": "^2.4.3",
        "body-parser": "^1.19.2",
        "cors": "^2.8.5",
        "crypto": "^1.0.1",
        "dotenv": "^16.0.0",
        "express": "^4.17.3",
        "jsonwebtoken": "^8.5.1",
        "moment": "^2.29.1",
        "mongoose": "^6.2.6",
        "nodemailer": "^6.7.3",
        "nodemailer-sendgrid-transport": "^0.2.0"
    }
}

what's wrong?

Upvotes: 1

Views: 1053

Answers (1)

Harshitha
Harshitha

Reputation: 7297

I have followed the same document which is provided and able to deploy the App to Azure App Service without any issues.

In package.json change the start to

"scripts": {
"start": "node ./bin/www"
},

As mentioned in the given document, after setting SCM_DO_BUILD_DURING_DEPLOYMENT command web.config will be generated in Azure App Service. But I don't see web.config file generated in your folder structure.

My Deployed folder Structure enter image description here

My autogenerated web.config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>  
    <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="bin/www" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^bin/www\/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{PATH_INFO}"/>
        </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="bin/www"/>
        </rule>
      </rules>
    </rewrite>   

    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <httpErrors existingResponse="PassThrough" />  
  </system.webServer>
</configuration>

My package.json

{
    "name": "myexpressapp",
    "version": "0.0.0",
    "private": true,
    "scripts": {
        "start": "node ./bin/www"
    },

"dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "ejs": "~2.6.1",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "morgan": "~1.9.1"
    }
}

My 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;

Make sure you have SCM_DO_BUILD_DURING_DEPLOYMENT setting available in Configuration => Application Settings after re-deploying the app from VSCode.

Deployed Azure App Output enter image description here

Upvotes: 1

Related Questions