How to create and deploy Node.js App with SCP Cloud Foundry

0
5253

Dear SAPLearners, in this blog post we will learn how to create and deploy Node.js App with SCP Cloud Foundry.

we will not go explain about Node.js and Cloud Foundry in this blog post, there are tons of material available on these topics to get better understanding.

💡 Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

💡 The Cloud Foundry environment allows you to create polyglot cloud applications in Cloud Foundry. It contains the SAP Cloud Platform Application Runtime service, which is based on the open-source application platform managed by the Cloud Foundry Foundation. –by SAP

Lets get started 😀

Prerequisites

To complete this tutorial, you will need:

  • Node.js installed on your laptop.
  • An IDE or text editor to use for editing files. I would recommend VSCode

Step-by-Step Procedure

1. First of all we want to do is create a folder “cf-nodejs-demo” in any of your location choice on your local machine. Let’s do this using the following windows command:

mkdir cf-nodejs-demo

2. Navigate to the folder, create a new folder “app” and initialize the a node project by using the following command:

npm init 

3. Provide the following information to initialize a Node.js project. You can hit enter for each option if you want default values to be set. Here is what i have provided

Create and Deploy NodeJS app with SCP Cloud Foundry CLI Step1

4. At this point, lets look at the folder structure of our Node.js project and contents of package.json file.

Create and Deploy NodeJS app with SCP Cloud Foundry CLI Step2

5. Its time to install our first Node.js package “express“. Lets do this by using following command:

You have to run this command inside “cf-nodejs-demo/app” folder.

npm install --save express

💡 Express is Fast, un-opinionated, minimalist web framework for node.

6. After package is successfully added, package.json file is updated with dependencies like below.

Create and Deploy NodeJS app with SCP Cloud Foundry CLI Step3

7. Now create a new JavaScript file inside the app folder and name it as index.js and copy the below code.


const express = require("express");
const app = express();

app.get("/", function (req, res) {
  res.send("Hello world! Cloud Foudnry Node Js Demo");
});

const port = process.env.PORT || 5000;
app.listen(port, function () {
  console.log("app listening at port " + port);
});

Above code is a basic node js code snippet.

8. Now that our code is completed and we are ready for test. To test the application run the following command in the terminal:

node index.js

9. At the terminal, you will see below message and if you open any browser and run this URL http://localhost:5000

Create and Deploy NodeJS app with SCP Cloud Foundry CLI Step5
Create and Deploy NodeJS app with SCP Cloud Foundry CLI Step4

10. If you are able to see above output without any errors you can congratulate yourself 🤝, as the Node.js project is ready to be deployed.

Deploy Node.js App to SCP Cloud Foundry 🚀

To deploy the Node.js App we are going to use Cloud Foundry CLI. Please find the installation guide here.

11. Create a manifest.yml file in the cf-nodejs-demo folder and copy the below code.

applications:
  - name: mynodeappdemo
    random-route: true
    path: app
    buidpack: https://github.com/cloudfoundry/nodejs-buidpack
    host: mynodeappdemo
    memory: 128M

💡 Manifests provide consistency and reproducibility, and can help you automate deploying apps. Both manifests and command line options allow you to override the default attribute values of cf push. You can more information here.

12. Before finally deploying the app, we need to add the start command in package.json file like below.

Create and Deploy NodeJS app with SCP Cloud Foundry CLI Step6

13. Finally, lets deploy the Node.js app with following command:

cf push

14. The deployment will start and will see the status getting displayed in the terminal like below

Create and Deploy NodeJS app with SCP Cloud Foundry CLI Step7

15. After Node.js App deployed to Cloud Foundry successfully you will be able to see the urls displayed in the terminal.

Create and Deploy NodeJS app with SCP Cloud Foundry CLI Step8

16. if you open the URL in browser, you should see similar output as Step-9.

Conclusion

Congrats!! you have successfully learnt the steps to create and deploy Node.js App with SCP Cloud Foundry.

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it ❤️, please share it! Thanks! 🙏