-1

I am developing a Node.JS project package.json and app.js (app.js is the main file of the project). I am using postgresql, I've placed all db related configuration under environmental variables.

I am trying to deploy the below project in Azure DevOps using CI/CD pipeline. This is a web app using Azure Repos.

Current Implementation:

Without CI/CD pipeline, following the below method to deploy the code and it's working fine.

Currently under Deployment Center Selected following items:

  1. Source: Azure Repos
  2. Azure Repos:
  3. Project: Project Name
  4. Repo: Repo name
  5. Branch: branch
  6. Build: Azure pipelines

Error I am getting after deployment:

In the logs I can see the deployment success, but when I hit the URL of the Node.js app, I see This Page isn't working right now or Internal server 500.

Steps I have followed:

  1. Step 1: I am using Azure Repo Please Note: I am using Classic Editor

  2. Step 2: Clicking pipelines (blade) --> pipelines --> New pipeline

  3. Step 2.1: Clicking pipeline (Build Pipeline) Entering the following fields:

    1. Name: Node-JS BackEnd Project
    2. Agent Pool: Azure pipelines
    3. Agent Specification: Windows Latest
  4. Step 2.2: Get sourceMapsEnabled (My-Repo and Branch)

  5. Step 2.3: Selecting AgentJob (npm Editing Following Fields):

    1. Task Version: 1
    2. Display Name: npm install
    3. Command: install
    4. Working folder that contains package.json: ./

NOTE: LEFT OTHER OPTIONS AS IT IS in Step 2.3

  1. Step 2.4: Selected Copy Files to (Edited Following Details):

    1. Task Version: 2
    2. Display Name: Copy Files to: ./
    3. Source Folder: ./
    4. Contents: ***.js, ***.json,
    5. Target Folder: ./

NOTE: LEFT OTHER OPTIONS AS IT IS in Step 2.4

  1. Step 2.5: Selected Archive Files LEFT ALL OPTIONS AS IT IS: 1.Archive Type: zip

  2. Step 2.6: Publish artifacts: drop LEFT ALL OPTIONS AS IT IS

After the above steps, I ran the build, which was successful, and I can see the updated code in artifacts.

  1. Step 3: In Pipelines Blade Clicked (Releases) Create Release.

  2. Step 3.1: Added by Artifact

  3. Step 3.2: Clicked Run On Agent and added (Azure App Service deploy)

  4. Step 3.3: Edited Following fields in Azure App Service deploy:

    1. Task Version: 4
    2. Display Name: App service deploy
    3. Connection Type: Azure Resource Manager
    4. Azure Subscription: xxxxxx
    5. App Service Type: Web app on Windows
    6. App Service name: node-js-backend-project
    7. Package or folder: $(System.DefaultWorkingDirectory)/**/*.zip

NOTE: OTHER OPTIONS IN Step 3.3 LEFT AS IT IS

After the above steps, I clicked create release - I can see all the jobs success. When I am hitting the backend URL I am getting the same issue (Internal server 500 error).

Code:

    //Package.json:
    {
    "name":"Node-backend-project",
    "version":"1.0.0",
    "main":"app.js"
    "scripts":{
        "start" : "nodemon app.js",
        "dev": "node app.js"
    }
    "dependencies":{
        "@azure/storage-blob":"^12.0.0",
        "axios":"^1.6.2",
        "cors":"^2.8.5",
        "dotenv":"^16.3.1",
        "express":"^4.18.1",
        "express-fileupload":"^1.4.3",
        "lowdb":"^1.0.0",
        "passport":"^0.6.0",
    }
    }
    //app.js
    const express = require("express");
    const cors = require("cors");
    const rateLimit = require("express-rate-limit");
    const router = require("./routes/index");
    const pool = require("./db");
    require("dotenv").config();
    const upload = require("express-fileupload");
    const { pipeline } = require("stream")
    const app = express();
    const port = process.env.PORT || 5000;
    const limiter = rateLimit({
        WindowMs:12*60*1000,
        max:70,
        standardHeaders:true,
        legacyHeaders:false,
    })
    
    app.use();
    app.use(cors());
    app.options('*', cors());
    app.use(express.json());
    app.use(express.urlencoded({extend:false}));
    app.use(router);
    app.use(upload());
    
    async function testConnection() {
    try {
        const client = await pool.conect();
        client.release();
    }catch(err){
        console.error("Error", err)
    }
    
    app.listen(port, async () => {
        await testConnection();
    })
    }

I have tried many solutions from StackOverflow and Microsoft documentation (where they use only Hello World program to deploy) but no solutions are working for me. Instead of Azure App Service, I tried with Azure Web App deploy also, but I am getting the same result. I have copied and pasted the web.config file (renamed the index.js to app.js) from the below StackOverflow link:

Azure Node.js Internal Server Error from index.js file

I am not clear on what I am doing wrong. From the front-end when I am hitting the dev URL I can see that I am getting internal server error (500 error).

Could anyone guide me to successfully host the Node.js app in Azure DevOps using CI/CD pipeline? Thanks in Advance!

1 Answer 1

0

These are my classic CI-CD steps to deploy a Node.js web app.

NodeJsWebAPP-CI

  1. Node.js tool installer

    enter image description here

    • Version Spec: Specific your target Node.js version
  2. Command line enter image description here

    npm install
    npm run build --if-present
    npm run test --if-present
  1. Archive files enter image description here

    • Root folder or file to archive: $(System.DefaultWorkingDirectory)
    • Archive file to create: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
    • Replace existing archive
  2. Publish Pipeline Artifacts enter image description here

    • File or directory path: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
    • Artifact name: Your artifact name

NodeJSWebAPP-CD

  1. Select your CI pipeline as the artifact enter image description here
  2. Azure App Service deploy enter image description here
    • Azure subscription: Your ARM service connection or Your Subscription.
    • App Service name: Your app name
    • Package or folder: $(System.DefaultWorkingDirectory)/**/*.zip
    • Additional Deployment Options->check Select deployment method
    • Deployment method: Web Deploy
    • Take App Offline: Uncheck
Sign up to request clarification or add additional context in comments.

4 Comments

I tried the above solution, I am getting App service is offline.
Hi @Rohan, have you unchecked the Take App Offline option of Azure App Service deploy task?
Hi @ Ziyang Liu-MSFT, yes I have unchecked the Take App offline
Please check if there is a custom app_offline.htm file in the root of your application. If there is, please remove it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.