Yamato DaiwaBackend

"Hello, world" — The Minimal Example

Pre-requirement Knowledge and Skills

It is implied that you are already can/know:

  • Create the Node.js project and install the npm dependencies (if no, you should to learn the Node.js fundamentals first).
  • Configure the TypeScript compiler (Yamato Daiwa Backend (from now: YDB) is intended to be used with TypeScript, herewith without any type).
  • The Object Orienting Programming (OOP) fundamentals: the approaches suggested by @yamato-daiwa/backend is OOP-based). Although YDB not impose the specific architecture, the usage of OOP is desirable, and therefore will be in the following lessons.

Node.js-project Rollout

Create the new Node.js project and install the following npm dependencies. During this lesson it is strongly recommended to install the versions specified inside the square brackets to avoid the troubles caused by changing of the API with new versions of these dependencies.

@yamato-daiwa/backend [0.3.0]
The main npm package of this framework
@yamato-daiwa/es-extensions [1.7.0]
Auxiliaries actual for both Node.js and Browser JavaScript. For this tutorial, we will need only HTTP_Methods enumeration from this package, but much more in the future.
ts-node [10.9.2]
The REPL for the TypeScript language. Creating the effect such that TypeScript has own runtime, thanks to what we will not spend our time to routines with output JavaScript files.
typescript [5.5.4]
The npm package package of the TypeScript language, the peer dependency of ts-node. Although modern versions of npm installs such dependencies automatically, we will consciously install the specific version of this package.

Code

Bellow server application will return the HTML code, including the heading h1 with text content "Hello, world!" on the HTTP request of GET-type to http://127.0.0.1:80/ address. Actually, such HTML code does not represent the valid HTML page, however for testing purposes the responding just with HTML body is possible — modern browsers will display it. Before submit this request, let us analyze the source code of this simplest example.

Testing

Let us launch our application by ts-node.

If the code has no mistakes, you will be informed about application successful launching by the following terminal output:

The log about successful start of the web application headed "The serving of HTTP requests started" is being displaying by green color in the console. The web application has been written by Node.js/TypeScript with "Yamato Daiwa Backend" framework. (abbreviation: "YDB").

Open the displaying URI in your browser. If your terminal can recognize the links, you can open it by mouse click. At least one log will be displayed, about request to root route:

The log about registration of get-request to the root route headed "New request" is being displaying by blue color. The web application has been written by Node.js/TypeScript with "Yamato Daiwa Backend" framework. (abbreviation: "YDB").

Probably one more request will be logged — about /favicon.ico route:

The log about registration of get-request to the "/favicon" route headed "New request" is being displaying by blue color in the console. The web application has been written by Node.js/TypeScript with "Yamato Daiwa Backend" framework. (abbreviation: "YDB").

If such log presents, it has been occurred by the browser automatically to display the favicon. If the framework was not prepared to such request, it submitted the response with "not found" error, however the YDB framework submitting the own icon as default. Of course it could be replaced with another one, what we will do in one one of subsequent lessons.

As localhost is the "pronoun" for the IP-address 127.0.0.1, and 80 is the default port for the HTTP protocol, besides http://127.0.0.1:80/ we can submit the requests to http://localhost, http://localhost:80 or http://127.0.0.1:80/.

Finally, let us see how the framework will behave when we will submit the request which has no the matching in routing. For example, in the request to http://127.0.0.1:80/foo case, the log will be:

The log about registration of get-request to the "/foo" route headed "New request" is being displaying by blue color in the console. Then, the log about reference to the unknown resource headed "Requested resource not found" is being displaying by red color in the console. The web application has been written by Node.js/TypeScript with "Yamato Daiwa Backend" framework. (abbreviation: "YDB").

The frameworks logged that the requested resource not found. The browser should display the similar message basing on response status 404 Not found.