In the previous article, we learnt about creating a Node.js web app in Azure using the App Service. In this article, we’ll learn about implementing the Node.js web app using the app service in Azure. This is the second article on the Azure App Service article series.
- Azure App Service - Creating Node.js Web App
- Azure App Service - Implementing Node.js Web App
Now, let us learn to implement the Node.js Web app in Azure.
Step 1
Follow up the Azure App Service - Creating Node.js Web App article and create the web app.
Step 2
Now, Click on the Cloud Shell button at the Top Menu bar aside of the Search Bar.
Step 3
The Bash terminal will now open at the bottom.
Step 4
Now, run the following commands.
cd ~
mkdir helloworld
This will switch the terminal to the root directory and create a directory named helloworld. You are free to name the directory any other name but you’ll have to follow up later on likewise.
cd helloworld
Now, change the directory and write the following code.
npm init -y
This will now create a package.json file which will now set up for our Node.js application.
Step 5
Now, let us create an index.js file with the following code.
touch index.js
Type the following code in the bash terminal and now, the interactive editor will open up.
code .
Here we can see, two files. One the index.js and other the package.json file
Step 6
Open the package.json file and make a small change on the “scripts” replacing the “test” for “start”.
{
"name": "helloworld",
...
"scripts": {
"start": "node index.js"
},
...
}
You can now save it with CTRL + S button.
Step 7
Next, open the index.js file and write the following Node.js program to provide a respond text for every GET request made in the server.
const http = require('http');
const server = http.createServer(function(request, response) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end("<html><body><h1>Hello C# Corner Reader - This is Ojash.</h1></body></html>");
});
const port = process.env.PORT || 1337;
server.listen(port);
console.log(`Server running at http://localhost:${port}`);
This will output my message on the port 1337.
Now, save the file with CTRL + S.
Step 8
Now, open another browser or tab and browse the website https://shell.azure.com/.
The terminal will open and now you need to change the directory to the helloworld one wil the command below.
cd ~/helloworld
Next, start NPM to start our web application.
npm start
Step 9
Now, on the primary cloud shell, write the following command to browse the web application we created.
curl http://127.0.0.1:1337/
Note that at the end, we have the same port we declared on the index.js file.
Here, as we run the command, we can see the output as follows which is the html code we wrote as a response on the index.js file
Thus, we have successfully implemented the Node.js web application now. At last, close the primary cloud shell with CTRL + C and also close the secondary shell we opened.
Conclusion
Thus, in this article, we learnt to implement the Node.js web application using the Azure App Service in the Azure Cloud Platform. In the following articles, we’ll learn to deploy and explore more things in the App Service.