1

Currently, I'm working on a project which is hosted on Microsoft Azure as a resource. The project is presented on a virtual machine and is operated using commands on the Azure CLI. Now I've been asked to create a web app for it using Node.js and React.js. I'm totally lost on how to connect the Node.js API to the virtual machine. Is there any way to trigger those Azure CLI commands through a Node.js app. Any help would be appreciated!

EDIT: Managed to solve the issue. Used this npm package 'ssh-exec' which lets you execute commands on a virtual machine remotely after connecting using IP Address, username, password. Very simple to use.

Link to package - https://www.npmjs.com/package/ssh-exec

2
  • Is the below answer provided by AjaykumarGhose-MT was helpful? If so could please accept the answer as solution. Commented Jun 23, 2021 at 4:40
  • @VenkateshDodda-MT Managed to solve the issue by myself. Have added my solution in the original post. Commented Jun 24, 2021 at 5:16

2 Answers 2

1

Managed to solve the issue. Used this npm package 'ssh-exec' which lets you execute commands on a virtual machine remotely after connecting using IP Address, username, password. Very simple to use.

Link to package - https://www.npmjs.com/package/ssh-exec

Sign up to request clarification or add additional context in comments.

Comments

0

There are some few steps to connect Node.js API to VM, Firstly, we need to clone the project that we will be deploying to the Azure VM. This project is a basic Node.js API with a single endpoint for returning an array of todo objects. Go to the location where you want to store the project and clone it:

git clone --single-branch --branch base-project https://github.com/coderonfleek/node-azure-vm.git

Once the project has been cloned, go to the root of the project and install dependencies:

cd node-azure-vm
npm install

Run the application using the npm run dev command. The application will start up at the address http://localhost:5000. When the application is up and running, enter http://localhost:5000/todos in your browser to see the list of todos.

enter image description here

Now, go to the package.json file of your project and add these scripts in the scripts sections:

"scripts": {
    .....,
    "stop": "pm2 kill",
    "start": "pm2 start server.js"
}

The start and stop scripts will use the pm2 process manager to start and stop the Node.js application on the VM. The pm2 script will be installed globally on the VM when it has been set up.

At the root of the project, run the rm -rf .git command to remove any .git history. Then push the project to GitHub. Make sure that this is the GitHub account connected to your CircleCI account.

Then, Setting up a virtual machine on Azure to run Node.js. Next, create a new VM on Azure and set its environment up for hosting the Node.js application. These are the steps:

  1. Create a new VM instance
  2. Install nginx
  3. Configure nginx to act as a proxy server. Route all traffic to port 80 on your VM to the running instance of the Node.js application on port 5000
  4. Install Node.js on the VM and clone the app from the GitHub repo into a folder in the VM
  5. Install pm2 globally

Do not be intimidated by the complexity of these steps! You can complete all five with one command. At the root of your project, create a new file named cloud-init-github.txt; it is a cloud-init file. Cloud-init is an industry-standard method for cloud instance initialization. cloud init- code

(REFER THE BELOW LINK FOR COMPLETE DETAILS)

https://circleci.com/blog/cd-azure-vm/

Comments

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.