This blog series is just a simple demo of how to create any Nodejs app and run it on both the local and SAP BTP platform.
Part – 1: Create nodejs application.
Part – 2: Create Authentication instance.
Create Local NodeJs application
First create folder basicnodejs.
Then inside create another folder srv, which will be for service providers.
Now from the terminal change the directory and go inside srv folder. we will need package.json file inside srv file.
Run command:
npm init --y
Now add/modify under scripts.
"start": "node server.js"
Whenever we run npm run start, it will execute the file server.js
We have to install express package- npm install express
Create a server.js file inside srv folder, and add the below code
Execute the command – npm run start
npm run start
Our node js application is running the static port 5000 that we have mentioned.
Now let’s create a simple mta.yaml file in the root directory and Provide the details to mta for deployment details for Service –
ID: basicnodejs
_schema-version: '3.1'
version: 0.0.1
parameters:
enable-parallel-deployments: true
modules:
- name: basicnodejs-service
type: nodejs
path: srv
build-parameters:
ignore:
- 'default-*.json'
- .env
- '*node_modules*'
- package-lock.json
Now build and deploy the mta.yaml to BTP. It will create a basicnodejs-service application under the dev space.
Click on the URL of the application.
We can access without any restriction from anywhere without having a BTP account.
So to make it restricted we will use approuter and authentication service in BTP.
Creating AppRouter:
For approuter, or to handle the routing, create app folder and package.json inside it
Install @sap/approuter inside app folder as a npm package.
npm install @sap/approuter
Now add the script to the package.json file –
"start": "node node_modules/@sap/approuter/approuter.js"
Now we have to create the file xs-app.json, which is required by approuter. That will handle the routes.
“srv-api” is a name that holds some information of a destination url.
Lets create the approuter module.
- name: basicnodejs-approuter
type: approuter.nodejs
path: app
build-parameters:
ignore:
- 'default-*.json'
- .env
- '*node_modules*'
- package-lock.json
parameters:
memory: 256M
disk-quota: 512M
keep-existing-routes: true
Now if we deployed the mta with these details only. It will crash.
Approuter will require some parameters which will be provided by the basicnodejs-service module.
ID: basicnodejs
_schema-version: '3.1'
version: 0.0.1
parameters:
enable-parallel-deployments: true
modules:
- name: basicnodejs-service
type: nodejs
path: srv
build-parameters:
ignore:
- 'default-*.json'
- .env
- '*node_modules*'
- package-lock.json
provides:
- name: srv-api
properties:
srv-url: ${default-url}
- name: basicnodejs-approuter
type: approuter.nodejs
path: app
build-parameters:
ignore:
- 'default-*.json'
- .env
- '*node_modules*'
- package-lock.json
parameters:
memory: 256M
disk-quota: 512M
keep-existing-routes: true
requires:
- name: srv-api
group: destinations
properties:
name: srv-api # must be used in xs-app.json as well
url: ~{srv-url}
basicnodejs-service provides configuration variables which will be required in basicnodejs-approuter
Now again build and deploy the mta.yaml
We can check the User-Provided Variables. and find the variables that we have set in the approuter requires section.
Now open the approuter url. We can access it without any credentials.
In this part, created two modules. which can be accessed by anyone on the internet.
We will create authentication to restrict the service module. Only through approuter the service module will be accessible.