"Server
" Class
The main class of the framework providing the functionality for processing of the HTTP requests including the HTTPS protocol support.
Public Static Methods
Initializes the server application with the settings passed via single parameter, then starts this application. In the simplest case, the code will be similar to the following one:
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import { HTTP_Methods } from "@yamato-daiwa/es-extensions";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
route: { HTTP_Method: HTTP_Methods.get, pathTemplate: "/" },
async handler(request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
Initializes the server application with the settings passed via single parameter,
but does not start this application.
It could be started later by the start
, the non-static
method.
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import { HTTP_Methods } from "@yamato-daiwa/es-extensions";
const serverApplication: Server = Server.initialize({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
route: { HTTP_Method: HTTP_Methods.get, pathTemplate: "/" },
async handler(request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
// ...
serverApplication.start();
Public Instance Methods
Launches the the instance of the server application represented by the instance of
the Server
class.
This instance must be created preliminarily by the
static method initialize
:
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import { HTTP_Methods } from "@yamato-daiwa/es-extensions";
const serverApplication: Server = Server.initialize({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
route: { HTTP_Method: HTTP_Methods.get, pathTemplate: "/" },
async handler(request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
// ...
serverApplication.start();
Related Types
Server.RawConfig
- IP_Address
- Required
- Yes
- Type
- HTTP
- Required
- No
- Type
- HTTPS
- Required
- No
- Type
Has an object type designed such as all that engineer must comprehend before starting the server application has been made required properties.
IP_Address
- Required
- Yes
- Type
The required property; must contain the valid IP address, on which the incoming HTTP requests will be listened . In the simplest case with single request handler and wihtout HTTPS protocol support, the minimal example will be:
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import { HTTP_Methods } from "@yamato-daiwa/es-extensions";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
route: { HTTP_Method: HTTP_Methods.get, pathTemplate: "/" },
async handler(request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
HTTP
- Required
- No
- Type
- port
- Required
- Yes
- Type
- number
Specify this settings group if you are planning to support the HTTP protocol.
Currently, the only setting of this group is the HTTP port number
(port
property).
To force the engineer to comprehend on which port the incoming HTTP requests
will be listened, this property has been make required.
If you are satisfied with default port for the HTTP protocol, use the
ProtocolDependentDefaultPorts
enumeration:
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import { HTTP_Methods } from "@yamato-daiwa/es-extensions";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
route: { HTTP_Method: HTTP_Methods.get, pathTemplate: "/" },
async handler(request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
HTTPS
- Required
- No
- Type
- port
- Required
- Yes
- Type
- number
- SSL_KeyFileRelativeOrAbsolutePath
- Required if
- "SSL_Key" property not specified
- Type
- string
- SSL_Key
- Required
- No
- Type
- string
- SSL_CertificateFileRelativeOrAbsolutePath
- Required if
- The "SSL_Certificate" property not specified
- Type
- string
- SSL_Certificate
- Required
- No
- Type
- string
Specify this settings group is you are planning to support the HTTPS protocol. According the modern security requirements, this protocol is exactly the one that should be used.
Currently, there are 5 properties among which there are 2 mutually exclusive pairs. Besides the HTTPS port number, it is required to specify the SSL certificate and SSL key, either via file path (relative or absolute, but the last one is more reliable) or via prepared strings representing the SSL certificate and SSL key. Below, the simplest examples for both cased represented.
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import { HTTP_Methods } from "@yamato-daiwa/es-extensions";
import Path from "path";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTPS: {
port: ProtocolDependentDefaultPorts.HTTPS,
SSL_CertificateFileRelativeOrAbsolutePath: Path.resolve(__dirname, "./SSL/SSL_Certificate.pem"),
SSL_KeyFileRelativeOrAbsolutePath: Path.resolve(__dirname, "./SSL/SSL_Key.pem")
},
routing: [
{
route: { HTTP_Method: HTTP_Methods.get, pathTemplate: "/" },
async handler(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import { HTTP_Methods } from "@yamato-daiwa/es-extensions";
import Path from "path";
import { secrets } from "docker-secret";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTPS: {
port: ProtocolDependentDefaultPorts.HTTPS,
SSL_Key: secrets.SSL_KEY,
SSL_Certificate: secrets.SSL_CERTIFICATE
},
routing: [
{
route: { HTTP_Method: HTTP_Methods.get, pathTemplate: "/" },
async handler(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
Certainly, the SSH key and SSH certificate must be prepared before launching the application.