API Tutorial Back End App in Node.Js
This tutorial will show you how to create a Back End app.
NOTE: In order to view videos in high resolution, please click on the Settings icon in YouTube, then select the highest resolution available.
Create a Node.Js Back End App
The purpose of this tutorial is to follow the steps to create a Back End app in Dev Center in order to be able to set up a Back End in our local.
First we have to go to Dev Center, once there we have selected the apps tab, and then we have to click the button of the new app.
Apps
New app
Then we have three types of apps, the creation of these is similar so this tutorial should be helpful for any of the three types, in our case we are gonna take as an example and gonna focus on a scheduler app.
Scheduler app
Now we have to write the name of our Back End app.
Select completion, check Frac, and choose natural time. These settings may be different according to your needs or requirements, but the whole process will be the same regardless of what you select as a configuration. Now we select a category, it could be any of these, I am going to select analytics.
Select category
Select analytics
The next step is to create a custom dataset that our app may be using. If the app needs a new dataset follow the next steps to create it, if not you can only select next:
Write a name for the dataset. Write a brief description of it. And as an example record we have to put a json object that simulates a model for the future objects in our dataset.
Create dataset
Now we see the final part of this wizard and we should select a language for our Back End app, In this case I am going to leave it as default, Node js.
Notice that we have a create-corva command here at the bottom part , we need to save that command, because in the future we will need it for the setup of the project in our local machine.
Corva command
After clicking Add New App button, we will have a Back End app created in dev center.
Back End app created
Back End Project Locally
This tutorial will show you how to setup a Back End Project Locally.
NOTE: In order to view videos in high resolution, please click on the Settings icon in YouTube, then select the highest resolution available.
Set up a Node.Js Back End App in our Local
Let’s start opening the terminal. Now we have to make sure we already have installed the pre-requisites:
-
Nodejs
-
Node package manager or npm
Package manager
After making sure we have these it is time to install Corva create app package that is going to help us with the project generation. For that we need to execute a npm install command, make sure to install the package globally and with administrator permissions, that is why in linux and in mac we should use sudo.
sudo npm i -g @corva/create-app
This is going to ask you for some metadata for your project, write some values or press enter to leave default.
Metadata values
We can verify installation using this command
create-corva-app –version
Now it is time to go to the folder where we want the project to be created. Once there it is time to paste the command that we copied from Dev Center when we created our app. In my case I have it here, and going to execute it (example) :
create-corva-app tutorialnodejs --appName “TutorialNodeJs” --segments “completion” --category “analytics” --appKey “sample.tutorialnodejs” --appType “scheduler” --schedulerType 1 --runtime “nodejs12.x”
Once it is done generating, a folder with default files will be created and we can start manipulating our project.
Default Project
This tutorial will explain the content of a Default Project.
NOTE: In order to view videos in high resolution, please click on the Settings icon in YouTube, then select the highest resolution available.
Explaining the Content of a Back End Node.Js Default App
Use a text editor to open the project root folder, Visual Studio Code is recommended for nodeJs Back End projects.
Now we are watching the default files for our project:
Default files
Let’s analyze some of the files we can find in a default Back End project:
Manifest.json: is our main file and it has the metadata for our application as we can see, also in the settings attribute we can find the configuration for our app to run in the cloud.
Corva apps are amazon web services lambda functions that run over data streams, so all this settings go directly to the lambda that is running our app, some settings are:
Entrypoint refers to the file and function where the execution will be triggered.
Timeout indicates the max time in seconds that our function can last before it times out.
Memory indicates the amount of memory in mb for our function, even though we cannot specify cpu power is good to know that the major amount of memory the major cpu power the cloud adds automatically to our app.
Environment object is useful to add all the environment key values that we may use throughout the code execution in value key pairs. Key pairs must be strings.
Runtime refers to the node js verison that is going to be used for our app.
Cron_string show us how often our scheduler app is going to be executed, each five minutes in this case.
Scheduler type value of 1means natural time.
In the datasets object we can add permissions for our app to access to some datasets in a Dev Center environment. This is important, let’s add permission to the test dataset we created in previous tutorials for this app.
Dev Center environment
test folder and src processor.js: are unit testing files for jest.js library, only useful for testing.
Package.json: contains the default commands for our project as well the default dependencies or packages added to our project.
Index.js: is the file where our code starts execution according to our manifest file. The handler function is the function to be triggered at the beginning of execution in our case using the Corva library scheduled method because our app is a scheduled app.
Let’s erase the default code inside the function, and write a hello world inside our handler.
Erase default code
For our manipulation we receive two objects in our handler function:
Event: for our function event data, we can get from this things like user data, asset_id, company_id and event api_key token for app authentication and more things.
Context: this one gives us instances of other corva libraries that can be very helpful through the development as api library, logger or cache.
Now we understand what we have in a default Back End project lets move to a deployment of our application.
Deploying a Back End App
This tutorial will show you how to Deploy a Back End App.
NOTE: In order to view videos in high resolution, please click on the Settings icon in YouTube, then select the highest resolution available.
Deploying a Node.Js Back End App
Open the terminal, in my case I am going to use Visual studio terminal, we have to be at the root folder of our application.
Now run the command npm install to generate package-lock.json file.
command npm install
After that we have to open the package.json file an look for the bundle command because we need to add some other files to the bundle: test folder and package-lock.json
Bundle command
Now we go back to the terminal to execute the npm run bundle command. It will generate a zip file, this is the one that we have to upload to Dev Center. Let’s reveal this zip file in finder or any file explorer you use.
Reveal zip file
Now we have to go to Dev Center
Look for our app
Go to the version tab.
And drag and drop our zip file.
Drag and drop zip file
It will ask to add some brief comment to our app version in order to identify or specify something important in this version.
Add brief comment
Then we gonna see the project listed and going through different statuses: queued > processing > deploying > draft
When our project is in a draft status we have to press the publish button to publish this version. Automatically the status is going to change from draft to published.
Publish
After that we add a label pressing the add button under the label column marking it as “Stable Release"
Add label
Now the last thing to do is to change the visibility of our project from default private to developer to developer status.
Visibility options
Run Back End App in Dev Center
This tutorial will show you how to tun a Back End app in Dev Center
NOTE: In order to view videos in high resolution, please click on the Settings icon in YouTube, then select the highest resolution available.
Running a Node.Js Back End App in Corva Dev Center
In order for an app to be triggered it has to be linked to a data stream, in this case a Corva well stream. To see the logs of our app we can go to the log files tab in our app explorer, but at the moment we have no logs since our app has not been executed.
Log files
So we need to link it to a well stream, these are the steps:
- Go to main Corva menu (upper left icon)
Dev Center
- Select the settings icon(upper right)
Settings icon
- Select the well streams menu
Well streams menu
- Make sure we are in completion well streams since our app is completion
Completion
- Select the well stream
Well stream
- Press add and look for our app
Look for app
7.Set the well stream “to active” by pressing the button
Select to active
Our app is a scheduler with a cron-job of each 5 minutes so it is going to be triggered each five minutes.
Now if we go to the log files tab in our app explorer we will be able to see logs each five minutes (each time our app is executed).
Log files tab