Create a Basic Node.js Application with Express Generator
Create a Node.js application skeleton with Express Generator and add some snippets into the backend as a basic application.
Overview
You will learn
- How to create a Node.js application skeleton with Node.js Express generator
- How to develop a particular endpoint to respond to client requests
Prerequisites
Prerequisites
- You have installed Node.js.
- You have a Kyma runtime environment on SAP Business Technology Platform (BTP). If not, please follow this tutorial: Enable SAP BTP, Kyma Runtime.
Steps
You can use the application generator tool Node.js Express generator to quickly create a Node.js application skeleton.
Before that, create a new directory for your project:
mkdir multitenancy-kyma-tutorialUnder the multitenancy-kyma-tutorial directory, create your own Node.js Application.
Option 1: Run the application generator to create application skeleton with single one npx command (available in Node.js 8.2.0):
npx express-generator --view=jade kyma-multitenant-nodeOption 2: Run the application generator to create application skeleton with command npm and express, by this way, packages will be saved locally as well.
Install the tool:
npm install -g express-generatorPackage
expressis installed together.
Verify if you have successfully installed the tool:
express --versionCreate an application skeleton:
express --view=jade kyma-multitenant-node
The generated app has the following directory structure:
.
โโโ app.js
โโโ bin
โย ย โโโ www
โโโ package.json
โโโ public
โย ย โโโ images
โย ย โโโ javascripts
โย ย โโโ stylesheets
โย ย โโโ style.css
โโโ routes
โย ย โโโ index.js
โย ย โโโ users.js
โโโ views
โโโ error.jade
โโโ index.jade
โโโ layout.jade
7 directories, 9 filesRouting 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).
Each route can have one or more handler functions, which are executed when the route is matched.
Route definition takes the following structure:
app.METHOD(PATH, HANDLER)Where:
appis an instance ofexpress.METHODis an HTTP request method, in lowercase.PATHis a path on the server.HANDLERis the function executed when the route is matched.
Add basic logic into the default get endpoint. To make the necessary changes you should navigate to kyma-multitenant-node/routes/index.js and add the logic with the following code snippets:
/**
* This is the default end-point when someone attempts to access the SaaS application.
* We show a message to the logged in user.
* Format of the message: Hello <logon name>; your tenant subdomain is <consumer sub-domain>; your tenant zone id is <consumer tenant id>
* The logon name will be specific to each user.
* The tenant zone and sub domain will be the same for all users of one consumer(tenant).
* Otherwise, if there is no AuthInfo object found, We show the message "Hello World" to users.
*/
router.get("/", function(req, res, next) {
try {
var line1 = "Hello " + req.authInfo.getLogonName();
var line2 = "your tenant sub-domain is " + req.authInfo.getSubdomain();
var line3 = "your tenant zone id is " + req.authInfo.getZoneId();
var responseMsg = line1 + "; " + line2 + "; " + line3;
res.send(responseMsg);
} catch (e) {
console.log("AuthInfo object undefined.");
var responseMsg = "Hello World!";
res.send(responseMsg);
}
});For more details on how to develop Express web application, please visit the official website: Express
You can find the final Node.js project from here.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.