|
| 1 | +--- |
| 2 | +title: Create a Basic Node.js Application with Express Generator |
| 3 | +description: Create a Node.js application skeleton with Express Generator and add some snippets into the backend as a basic application. |
| 4 | +auto_validation: true |
| 5 | +time: 25 |
| 6 | +tags: [ tutorial>beginner, programming-tool>node-js] |
| 7 | +primary_tag: software-product>sap-btp\, kyma-runtime |
| 8 | +--- |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | +- You have installed [Node.js](https://nodejs.org/en/download/). |
| 12 | +- You have a Kyma runtime environment on SAP Business Technology Platform (BTP). If not, please follow this tutorial: [Enable SAP BTP, Kyma Runtime](cp-kyma-getting-started). |
| 13 | + |
| 14 | +## Details |
| 15 | +### You will learn |
| 16 | +- How to create a Node.js application skeleton with Node.js Express generator |
| 17 | +- How to develop a particular endpoint to respond to client requests |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +[ACCORDION-BEGIN [Step 1: ](Create Node.js Application Skeleton)] |
| 22 | + |
| 23 | +You can use the application generator tool [Node.js Express generator](https://expressjs.com/en/starter/generator.html) to quickly create a Node.js application skeleton. |
| 24 | + |
| 25 | +Before that, create a new directory for your project: |
| 26 | +``` |
| 27 | +mkdir multitenancy-kyma-tutorial |
| 28 | +``` |
| 29 | + |
| 30 | +Under the `multitenancy-kyma-tutorial` directory, create your own Node.js Application. |
| 31 | + |
| 32 | +**Option 1**: Run the application generator to create application skeleton with single one `npx` command (available in Node.js 8.2.0): |
| 33 | +``` |
| 34 | +npx express-generator --view=jade kyma-multitenant-node |
| 35 | +``` |
| 36 | + |
| 37 | +**Option 2**: Run the application generator to create application skeleton with command `npm` and `express`, by this way, packages will be saved locally as well. |
| 38 | + |
| 39 | +Install the tool: |
| 40 | + |
| 41 | +``` |
| 42 | +npm install -g express-generator |
| 43 | +``` |
| 44 | + |
| 45 | +> Package `express` is installed together. |
| 46 | +
|
| 47 | +Verify if you have successfully installed the tool: |
| 48 | + |
| 49 | +``` |
| 50 | +express --version |
| 51 | +``` |
| 52 | + |
| 53 | +Create an application skeleton: |
| 54 | + |
| 55 | +``` |
| 56 | +express --view=jade kyma-multitenant-node |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +The generated app has the following directory structure: |
| 62 | + |
| 63 | +``` |
| 64 | +. |
| 65 | +├── app.js |
| 66 | +├── bin |
| 67 | +│ └── www |
| 68 | +├── package.json |
| 69 | +├── public |
| 70 | +│ ├── images |
| 71 | +│ ├── javascripts |
| 72 | +│ └── stylesheets |
| 73 | +│ └── style.css |
| 74 | +├── routes |
| 75 | +│ ├── index.js |
| 76 | +│ └── users.js |
| 77 | +└── views |
| 78 | + ├── error.jade |
| 79 | + ├── index.jade |
| 80 | + └── layout.jade |
| 81 | +
|
| 82 | +7 directories, 9 files |
| 83 | +``` |
| 84 | + |
| 85 | +[DONE] |
| 86 | +[ACCORDION-END] |
| 87 | + |
| 88 | + |
| 89 | +[ACCORDION-BEGIN [Step 2: ](Add Code Snippets into Node.js Application)] |
| 90 | + |
| 91 | +Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on). |
| 92 | + |
| 93 | +Each route can have one or more handler functions, which are executed when the route is matched. |
| 94 | + |
| 95 | +Route definition takes the following structure: |
| 96 | + |
| 97 | +```javascript |
| 98 | +app.METHOD(PATH, HANDLER) |
| 99 | +``` |
| 100 | + |
| 101 | +Where: |
| 102 | + |
| 103 | +- `app` is an instance of `express`. |
| 104 | +- `METHOD` is an [HTTP request method](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods), in lowercase. |
| 105 | +- `PATH` is a path on the server. |
| 106 | +- `HANDLER` is the function executed when the route is matched. |
| 107 | + |
| 108 | +Add basic logic into the default get endpoint with the following code snippets: |
| 109 | + |
| 110 | +```javascript |
| 111 | +/** |
| 112 | + * This is the default end-point when someone attempts to access the SaaS application. |
| 113 | + * We show a message to the logged in user. |
| 114 | + * Format of the message: Hello <logon name>; your tenant subdomain is <consumer sub-domain>; your tenant zone id is <consumer tenant id> |
| 115 | + * The logon name will be specific to each user. |
| 116 | + * The tenant zone and sub domain will be the same for all users of one consumer(tenant). |
| 117 | + * Otherwise, if there is no AuthInfo object found, We show the message "Hello World" to users. |
| 118 | + */ |
| 119 | +router.get("/", function(req, res, next) { |
| 120 | + try { |
| 121 | + var line1 = "Hello " + req.authInfo.getLogonName(); |
| 122 | + var line2 = "your tenant sub-domain is " + req.authInfo.getSubdomain(); |
| 123 | + var line3 = "your tenant zone id is " + req.authInfo.getZoneId(); |
| 124 | + var responseMsg = line1 + "; " + line2 + "; " + line3; |
| 125 | + res.send(responseMsg); |
| 126 | + } catch (e) { |
| 127 | + console.log("AuthInfo object undefined."); |
| 128 | + var responseMsg = "Hello World!"; |
| 129 | + res.send(responseMsg); |
| 130 | + } |
| 131 | +}); |
| 132 | +``` |
| 133 | + |
| 134 | +> For more details on how to develop Express web application, please visit the official website: [Express](https://expressjs.com/) |
| 135 | +
|
| 136 | +[VALIDATE_1] |
| 137 | +[ACCORDION-END] |
| 138 | + |
| 139 | +[ACCORDION-BEGIN [Step 3: ](Project Repository)] |
| 140 | + |
| 141 | + |
| 142 | +You can find the final Node.js project from repository: [here](https://github.com/SAP-samples/btp-kyma-runtime-multitenancy-tutorial/tree/main/Mission:%20Develop%20a%20Node.js%20Application%20in%20the%20SAP%20BTP%20Kyma%20Runtime). |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +[DONE] |
| 147 | +[ACCORDION-END] |
| 148 | + |
| 149 | + |
| 150 | +--- |
0 commit comments