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
+69-48Lines changed: 69 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,37 @@
1
1
---
2
+
parser: v2
2
3
author_name: Gergana Tsakova
3
4
author_profile: https://github.com/Joysie
4
-
title: Create a Node.js Application via Cloud Foundry Command Line Interface
5
-
description: Create a simple Node.js application in the Cloud Foundry Command Line Interface (cf CLI) and enable services for it.
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 with Cloud Foundry Node.js Buildpack
14
+
<!-- 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).
15
+
16
+
## You will learn
17
+
- How to create a simple "Hello World" application in Node.js
18
+
- How to create an application router for it
19
+
- How to run authentication and authorization checks via the XSUAA service
20
+
12
21
## Prerequisites
13
-
- 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).
22
+
- 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).
14
23
- You have created a subaccount and a space on Cloud Foundry Environment.
15
24
-[cf CLI] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/4ef907afb1254e8286882a2bdef0edf4.html) is installed locally.
16
-
-[Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally.
25
+
-[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.
17
26
- You have installed an integrated development environment, for example [Visual Studio Code] (https://code.visualstudio.com/).
18
27
19
-
## Details
20
-
### You will learn
21
-
- How to create a simple "Hello World" application in Node.js
22
-
- How to run authentication checks via XSUAA service
23
-
- How to run authorization checks by setting XSUAA scopes
24
-
25
-
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).
28
+
## Intro
29
+
This tutorial will guide you through creating and setting up a simple Node.js application in 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 a web microservice (application router). Finally, you will set authentication checks and authorization roles to properly access and manage your web application.
27
30
28
31
---
29
32
30
-
[ACCORDION-BEGIN [Step 1: ](Log on to SAP BTP)]
33
+
### Log on to SAP BTP
34
+
31
35
32
36
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)
33
37
@@ -46,18 +50,21 @@ In this tutorial, we use `eu20.hana.ondemand.com` as an example.
46
50
cf login
47
51
```
48
52
49
-
4. When prompted, enter your user credentials – the email and password you have used to register your productive SAP BTP account.
53
+
4. When prompted, enter your user credentials – the email and password you have used to register your trial or productive SAP BTP account.
54
+
55
+
> **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).
56
+
57
+
58
+
5. Choose the org name and space where you want to create your application.
50
59
51
-
> **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).
52
60
53
61
#### RESULT
54
62
55
63
Details about your personal SAP BTP subaccount are displayed (API endpoint, user, organization, space).
56
64
57
-
[DONE]
58
-
[ACCORDION-END]
59
65
60
-
[ACCORDION-BEGIN [Step 2: ](Create a Node.js application)]
66
+
### Create a Node.js application
67
+
61
68
62
69
You're going to create a simple Node.js application.
63
70
@@ -82,7 +89,7 @@ You're going to create a simple Node.js application.
82
89
83
90
> **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.
84
91
85
-
> 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`
92
+
> 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`
86
93
87
94
4. Inside `node-tutorial`, create a subfolder `myapp`.
88
95
@@ -92,7 +99,7 @@ You're going to create a simple Node.js application.
92
99
npm init
93
100
```
94
101
95
-
This will walk you through creating a `package.json` file in the `myapp` folder. Press **Enter** on every step.
102
+
Press **Enter** on every step. This process will walk you through creating a `package.json` file in the `myapp` folder.
96
103
97
104
6. Then, still in the `myapp` directory, execute:
98
105
@@ -116,7 +123,7 @@ You're going to create a simple Node.js application.
116
123
"author": "",
117
124
"license": "ISC",
118
125
"dependencies": {
119
-
"express": "^4.18.1"
126
+
"express": "^4.18.2"
120
127
}
121
128
}
122
129
```
@@ -138,7 +145,7 @@ You're going to create a simple Node.js application.
138
145
"author": "",
139
146
"license": "ISC",
140
147
"dependencies": {
141
-
"express": "^4.18.1"
148
+
"express": "^4.18.2"
142
149
}
143
150
}
144
151
```
@@ -167,23 +174,23 @@ You're going to create a simple Node.js application.
167
174
cf push
168
175
```
169
176
170
-
> Make sure you always execute `cf push` in the folder where the `manifest.yml` file is located! In this case, that's `node-tutorial`.
177
+
> Make sure you always execute `cf push` in the directory where the `manifest.yml` file is located! In this case, that's `node-tutorial`.
171
178
172
179
10. When the staging and deployment steps are completed, the `myapp` application should be successfully started and its details displayed in the command console.
173
180
174
-
11. Now open a browser window and enter the URL of the `myapp` application (see the route).
181
+
11. Open a browser window and enter the URL of the `myapp` application (see the route).
175
182
176
183
That is: `https://node-1234-aaaa-5678.cfapps.eu20.hana.ondemand.com`
177
184
178
185
#### RESULT
179
186
180
187
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.
181
188
182
-
[VALIDATE_1]
183
-
[ACCORDION-END]
184
189
185
190
186
-
[ACCORDION-BEGIN [Step 3: ](Run an Authentication Check)]
191
+
192
+
### Run an Authentication Check
193
+
187
194
188
195
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.
189
196
@@ -196,6 +203,22 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
196
203
}
197
204
```
198
205
206
+
> **NOTE**: 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:
200
223
201
224
```Bash/Shell
@@ -219,7 +242,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
219
242
220
243
The `nodeuaa` service instance will be bound to the `myapp` application during deployment.
221
244
222
-
4. Now you have to create a microservice (the application router). Go to the `node-tutorial` folder and create a subfolder `web`.
245
+
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`.
223
246
224
247
> **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.
225
248
@@ -234,7 +257,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
@@ -247,7 +270,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
247
270
npm init
248
271
```
249
272
250
-
This will walk you through creating a `package.json` file in the `web` folder. Press **Enter** on every step.
273
+
Press **Enter** on every step. This process will walk you through creating a `package.json` file in the `web` folder.
251
274
252
275
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:
253
276
@@ -350,7 +373,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
350
373
351
374
> ### What's going on?
352
375
353
-
>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.
376
+
> 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.
354
377
355
378
356
379
13. When the staging and deployment steps are completed, the `web` application should be successfully started and its details displayed in the command console.
@@ -365,15 +388,15 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
365
388
366
389
#### RESULT
367
390
368
-
- 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.
391
+
- 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.
369
392
370
393
- 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`.
371
394
372
395
373
-
[VALIDATE_2]
374
-
[ACCORDION-END]
375
396
376
-
[ACCORDION-BEGIN [Step 4: ](Run an Authorization Check)]
397
+
398
+
### Run an Authorization Check
399
+
377
400
378
401
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)
379
402
@@ -413,6 +436,8 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
413
436
}
414
437
```
415
438
439
+
> For trial accounts, adjust the code respectively.
440
+
416
441
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.
417
442
418
443
@@ -437,7 +462,7 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
437
462
438
463
This will be the initial list of users for the REST service.
439
464
440
-
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:
465
+
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:
441
466
442
467
```Bash/Shell
443
468
npm install body-parser --save
@@ -494,7 +519,7 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
494
519
495
520
> **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)
496
521
497
-
> 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.
522
+
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.
498
523
499
524
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:
500
525
@@ -564,18 +589,18 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
564
589
565
590
This command will update both applications (`myapp` and `web`).
566
591
567
-
8. Try to access `myapp` again (in a browser) in both ways – directly, and through the `web` application router.
592
+
8. Try to access `myapp` again (in a browser) in both ways – directly and through the `web` application router.
568
593
569
594
#### RESULT
570
595
571
-
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.
596
+
- When you access it directly, you should still get a response `401 Unauthorized`. This is a correct and expected behavior.
597
+
- 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.
572
598
573
599
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.
574
600
575
-
[DONE]
576
-
[ACCORDION-END]
577
601
578
-
[ACCORDION-BEGIN [Step 5: ](Assigning Roles to a User in SAP BTP Cockpit)]
602
+
### Assigning Roles to a User in SAP BTP Cockpit
603
+
579
604
580
605
1. Open the SAP BTP cockpit and go to your subaccount.
581
606
@@ -597,7 +622,7 @@ To get permissions, you need to create a role collection containing the roles `V
597
622
598
623
> Your role collection is now assigned to your user and contains the roles you need to view and manage the content of your application.
599
624
600
-
> Now you need to apply these changes to the `myapp` application by redeploying it again.
625
+
Now you need to apply these changes to the `myapp` application by redeploying it again.
601
626
602
627
10. Go back to the command line, and in the `node-tutorial` directory execute:
603
628
@@ -610,13 +635,9 @@ To get permissions, you need to create a role collection containing the roles `V
610
635
611
636
Accessing the `myapp` application results in the following:
612
637
613
-
- 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.
638
+
- 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.
614
639
615
640
- 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