You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/btp-cf-buildpacks-node-create/btp-cf-buildpacks-node-create.md
+51-53Lines changed: 51 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -2,36 +2,30 @@
2
2
parser: v2
3
3
author_name: Gergana Tsakova
4
4
author_profile: https://github.com/Joysie
5
+
title: Create a Node.js Application with Cloud Foundry Node.js Buildpack
6
+
description: Create a simple application and enable services for it, by using the Cloud Foundry Node.js Buildpack and Cloud Foundry Command Line Interface (cf CLI).
# Create a Node.js Application via Cloud Foundry Command Line Interface
12
-
<!-- description --> Create a simple Node.js application in the Cloud Foundry Command Line Interface (cf CLI) and enable services for it.
13
+
## You will learn
14
+
- How to create a simple "Hello World" application in Node.js
15
+
- How to create an application router for it
16
+
- How to run authentication and authorization checks via the XSUAA service
13
17
14
18
## Prerequisites
15
-
- You have a productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
19
+
- You have a trial or a productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
16
20
- You have created a subaccount and a space on Cloud Foundry Environment.
17
21
-[cf CLI] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/4ef907afb1254e8286882a2bdef0edf4.html) is installed locally.
18
-
-[Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally.
22
+
-[Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally. Make sure you have the latest Node.js version. In this tutorial, we use v.18.
19
23
- You have installed an integrated development environment, for example [Visual Studio Code] (https://code.visualstudio.com/).
20
24
21
-
## You will learn
22
-
- How to create a simple "Hello World" application in Node.js
23
-
- How to run authentication checks via XSUAA service
24
-
- How to run authorization checks by setting XSUAA scopes
25
-
26
-
27
25
## Intro
28
26
This tutorial will guide you through creating and setting up a simple Node.js application by using cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through another one - a web microservice (application router).
29
27
30
-
---
31
-
32
28
### Log on to SAP BTP
33
-
34
-
35
29
First, you need to connect to the SAP BTP, Cloud Foundry environment with your productive subaccount. Your Cloud Foundry URL depends on the region where the API endpoint belongs to. To find out which one is yours, see: [Regions and API Endpoints Available for the CF Environment] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/f344a57233d34199b2123b9620d0bb41.html?version=Cloud)
36
30
37
31
In this tutorial, we use `eu20.hana.ondemand.com` as an example.
@@ -49,18 +43,19 @@ In this tutorial, we use `eu20.hana.ondemand.com` as an example.
49
43
cf login
50
44
```
51
45
52
-
4. When prompted, enter your user credentials – the email and password you have used to register your productive SAP BTP account.
46
+
4. When prompted, enter your user credentials – the email and password you have used to register your trial or productive SAP BTP account.
47
+
48
+
> **IMPORTANT**: If the authentication fails, even though you've entered correct credentials, try [logging in via single sign-on] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/e1009b4aa486462a8951c4d499ce6d4c.html?version=Cloud).
53
49
54
-
> **IMPORTANT**: If the authentication fails, even though you've entered correct credentials, try [logging in via single sign-on] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/e1009b4aa486462a8951c4d499ce6d4c.html?version=Cloud).
55
50
56
-
#### RESULT
51
+
5. Choose the org name and space where you want to create your application.
52
+
57
53
54
+
#### RESULT
58
55
Details about your personal SAP BTP subaccount are displayed (API endpoint, user, organization, space).
59
56
60
57
61
58
### Create a Node.js application
62
-
63
-
64
59
You're going to create a simple Node.js application.
65
60
66
61
1. In your local file system, create a new directory (folder). For example: `node-tutorial`
@@ -84,7 +79,7 @@ You're going to create a simple Node.js application.
84
79
85
80
> **IMPORTANT**: Make sure you don't have another application with the name `myapp` in your space! If you do, use a different name and adjust the whole tutorial according to it.
86
81
87
-
> Also bear in mind that your application's technical name (in the route) must be **unique** in the whole Cloud Foundry landscape. We advice that you use, for example, your subdomain name or part of your subaccount ID to construct the technical name. In this tutorial, we use: `node-1234-aaaa-5678`
82
+
> Also, your application's technical name (in the route) must be **unique** in the whole Cloud Foundry landscape. We advice that you use, for example, your subdomain name or part of your subaccount ID to construct the technical name. In this tutorial, we use: `node-1234-aaaa-5678`
88
83
89
84
4. Inside `node-tutorial`, create a subfolder `myapp`.
90
85
@@ -94,7 +89,7 @@ You're going to create a simple Node.js application.
94
89
npm init
95
90
```
96
91
97
-
This will walk you through creating a `package.json` file in the `myapp` folder. Press **Enter** on every step.
92
+
Press **Enter** on every step. This process will walk you through creating a `package.json` file in the `myapp` folder.
98
93
99
94
6. Then, still in the `myapp` directory, execute:
100
95
@@ -118,7 +113,7 @@ You're going to create a simple Node.js application.
118
113
"author": "",
119
114
"license": "ISC",
120
115
"dependencies": {
121
-
"express": "^4.18.1"
116
+
"express": "^4.18.2"
122
117
}
123
118
}
124
119
```
@@ -140,7 +135,7 @@ You're going to create a simple Node.js application.
140
135
"author": "",
141
136
"license": "ISC",
142
137
"dependencies": {
143
-
"express": "^4.18.1"
138
+
"express": "^4.18.2"
144
139
}
145
140
}
146
141
```
@@ -169,24 +164,21 @@ You're going to create a simple Node.js application.
169
164
cf push
170
165
```
171
166
172
-
> Make sure you always execute `cf push` in the folder where the `manifest.yml` file is located! In this case, that's `node-tutorial`.
167
+
> Make sure you always execute `cf push` in the directory where the `manifest.yml` file is located! In this case, that's `node-tutorial`.
173
168
174
169
10. When the staging and deployment steps are completed, the `myapp` application should be successfully started and its details displayed in the command console.
175
170
176
-
11. Now open a browser window and enter the URL of the `myapp` application (see the route).
171
+
11. Open a browser window and enter the URL of the `myapp` application (see the route).
177
172
178
173
That is: `https://node-1234-aaaa-5678.cfapps.eu20.hana.ondemand.com`
179
174
180
175
#### RESULT
181
-
182
176
Your Node.js application is successfully deployed and running on the SAP BTP, Cloud Foundry environment. A **Hello World!** message is displayed in the browser.
183
177
184
178
185
179
186
180
187
181
### Run an Authentication Check
188
-
189
-
190
182
Authentication in the SAP BTP, Cloud Foundry environment is provided by the Authorization and Trust Management (XSUAA) service. In this example, OAuth 2.0 is used as the authentication mechanism. The simplest way to add authentication is to use the Node.js `@sap/approuter` package. To do that, a separate Node.js micro-service will be created, acting as an entry point for the application.
191
183
192
184
1. In the `node-tutorial` folder, create an `xs-security.json` file for your application with the following content:
@@ -198,6 +190,22 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
198
190
}
199
191
```
200
192
193
+
> **IMPORTANT**: For trial accounts, enter the following additional `oauth2-configuration` lines in your `xs-security.json` file:
2. Create an `xsuaa` service instance named `nodeuaa` with plan `application`. To do that, execute the following command in the `node-tutorial` directory:
202
210
203
211
```Bash/Shell
@@ -221,7 +229,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
221
229
222
230
The `nodeuaa` service instance will be bound to the `myapp` application during deployment.
223
231
224
-
4. Now you have to create a microservice (the application router). Go to the `node-tutorial` folder and create a subfolder `web`.
232
+
4. Now you have to create a microservice (the application router). To do that, go to the `node-tutorial` folder and create a subfolder `web`.
225
233
226
234
> **IMPORTANT**: Make sure you don't have another application with the name `web` in your space! If you do, use a different name and adjust the rest of the tutorial according to it.
227
235
@@ -236,7 +244,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
236
244
</head>
237
245
<body>
238
246
<h1>Node.js Tutorial</h1>
239
-
<a href="/myapp/">My Application</a>
247
+
<a href="/myapp/">My Node.js Application</a>
240
248
</body>
241
249
</html>
242
250
```
@@ -249,7 +257,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
249
257
npm init
250
258
```
251
259
252
-
This will walk you through creating a `package.json` file in the `web` folder. Press **Enter** on every step.
260
+
Press **Enter** on every step. This process will walk you through creating a `package.json` file in the `web` folder.
253
261
254
262
8. Now you need to create a directory `web/node_modules/@sap` and install an `approuter` package in it. To do that, in the `web` directory execute:
255
263
@@ -352,7 +360,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
352
360
353
361
> ### What's going on?
354
362
355
-
>As of this point of the tutorial, the URL of the `web` application will be requested instead of the `myapp` URL. It will then forward the requests to the `myapp` application.
363
+
> At this point of the tutorial, the URL of the `web` application will be requested instead of the `myapp` URL. It will then forward the requests to the `myapp` application.
356
364
357
365
358
366
13. When the staging and deployment steps are completed, the `web` application should be successfully started and its details displayed in the command console.
@@ -366,17 +374,14 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
366
374
> Both the `myapp` and `web` applications are bound to the same Authorization and Trust Management (XSUAA) service instance `nodeuaa`. In this scenario, the authentication is handled by XSUAA through the application router.
367
375
368
376
#### RESULT
369
-
370
-
- Click the `My Application` link. The browser window displays **Application user:** `<e-mail>`, where `<e-mail>` is the one you have logged to Cloud Foundry with.
377
+
- Click the `My Node.js Application` link. The browser window displays **Application user:** `<e-mail>`, showing the email you have used for your Cloud Foundry logon.
371
378
372
379
- Check that the `myapp` application is not accessible without authentication. To do that, refresh its previously loaded URL in a web browser – you should get a response `401 Unauthorized`.
373
380
374
381
375
382
376
383
377
384
### Run an Authorization Check
378
-
379
-
380
385
Authorization in the SAP BTP, Cloud Foundry environment is also provided by the XSUAA service. In the previous example, the `@sap/approuter` package was added to provide a central entry point for the business application and to enable authentication. Now to extend the example, authorization will be added through the implementation of a `users` REST service. Different authorization checks will be introduced for the GET and CREATE operations to demonstrate how authorization works. The authorization concept includes elements such as roles, scopes, and attributes provided in the security descriptor file `xs-security.json`. For more information, see: [Application Security Descriptor Configuration Syntax] (https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/517895a9612241259d6941dbf9ad81cb.html?version=Cloud)
381
386
382
387
1. To introduce application roles, open the `xs-security.json` in the `node-tutorial` folder, and add scopes and role templates as follows:
@@ -415,6 +420,8 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
415
420
}
416
421
```
417
422
423
+
> For trial accounts, adjust the code respectively.
424
+
418
425
Two roles (`Viewer` and `Manager`) are introduced. These roles represent sets of OAuth 2.0 scopes or actions. The scopes are used later in the microservice's code for authorization checks.
419
426
420
427
@@ -439,7 +446,7 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
439
446
440
447
This will be the initial list of users for the REST service.
441
448
442
-
4. You need to add a dependency to `body-parser` that will be used for JSON parsing. To do that, in the `myapp` folder, execute:
449
+
4. You need to add a dependency to `body-parser` that will be used for JSON parsing. To do that, in the `myapp` directory, execute:
443
450
444
451
```Bash/Shell
445
452
npm install body-parser --save
@@ -494,9 +501,9 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
494
501
});
495
502
```
496
503
497
-
> **NOTE:** Authorization checks are enforced by the `xssec` package in the `@sap` directory. To every request object, using `passport` and `xssec.JWTStrategy`, a security context is attached as an `authInfo` object. The resulting request object is initialized with the incoming JWT token. To check the full list of methods and properties of the security context, see: [Authentication for Node.js Applications] (https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/4902b6e66cbd42648b5d9eaddc6a363d.html?version=Cloud)
504
+
> Authorization checks are enforced by the `xssec` package in the `@sap` directory. To every request object, using `passport` and `xssec.JWTStrategy`, a security context is attached as an `authInfo` object. The resulting request object is initialized with the incoming JWT token. To check the full list of methods and properties of the security context, see: [Authentication for Node.js Applications] (https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/4902b6e66cbd42648b5d9eaddc6a363d.html?version=Cloud)
498
505
499
-
> As defined in the `start.js` file, for HTTP GET requests users need the `Display` scope to be authorized. For HTTP POST requests, they need to have the `Update` scope assigned.
506
+
As defined in the `start.js` file, for HTTP GET requests users need the `Display` scope to be authorized. For HTTP POST requests, they need to have the `Update` scope assigned.
500
507
501
508
6. Update the UI to be able to send POST requests. To do that, go to `web>resources` and in the `index.html` file, replace the content with the following code:
502
509
@@ -566,18 +573,16 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
566
573
567
574
This command will update both applications (`myapp` and `web`).
568
575
569
-
8. Try to access `myapp` again (in a browser) in both ways – directly, and through the `web` application router.
576
+
8. Try to access `myapp` again (in a browser) in both ways – directly and through the `web` application router.
570
577
571
578
#### RESULT
572
-
573
-
When you access the `web` application and click the `Show users` link, it should result in a `403 Forbidden` response due to missing permissions. The same error is thrown if you try to add a new user.
579
+
- When you access it directly, you should still get a response `401 Unauthorized`. This is a correct and expected behavior.
580
+
- When you access the `web` application and click the `Show users` link, it should result in a `403 Forbidden` response due to missing permissions. The same error is thrown if you try to add a new user.
574
581
575
582
To get permissions, you need to create a role collection containing the roles `Viewer` and `Manager` and assign these roles to your user. You can do this only from the SAP BTP cockpit.
576
583
577
584
578
585
### Assigning Roles to a User in SAP BTP Cockpit
579
-
580
-
581
586
1. Open the SAP BTP cockpit and go to your subaccount.
582
587
583
588
2. From the left-side menu, navigate to `Security` > `Role Collections`.
@@ -598,7 +603,7 @@ To get permissions, you need to create a role collection containing the roles `V
598
603
599
604
> Your role collection is now assigned to your user and contains the roles you need to view and manage the content of your application.
600
605
601
-
> Now you need to apply these changes to the `myapp` application by redeploying it again.
606
+
Now you need to apply these changes to the `myapp` application by redeploying it again.
602
607
603
608
10. Go back to the command line, and in the `node-tutorial` directory execute:
604
609
@@ -608,15 +613,8 @@ To get permissions, you need to create a role collection containing the roles `V
608
613
609
614
610
615
#### RESULT
611
-
612
616
Accessing the `myapp` application results in the following:
613
617
614
-
- If you try to access it directly, a response `403 Forbidden` is displayed due to lack or permissions (roles). This is a correct and expected behavior.
618
+
- If you try to access it directly, a response `401 Unauthorized` is still displayed due to lack or permissions (roles). This is a correct and expected behavior.
615
619
616
620
- If you try to access it through the `web` application router, the `Show users` link will show the list of users - **John** and **Paula**. If you enter a new name, it will be successfully recorded in the user database.
0 commit comments