"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
initializeAndStart
- Generic technical name
- rawConfiguration
- Type
- Server.RawConfiguration
- IP_Address
- Type
- string
- Is Undefined Forbidden
- Yes
- Is Null Forbidden
- Yes
- HTTP
- Type
- RawConfig.HTTP
- Is Undefined Forbidden
- No
- Is Null Forbidden
- Yes
- HTTPS
- Type
- RawConfig.HTTPS
- Is Undefined Forbidden
- No
- Is Null Forbidden
- Yes
- routing
- Type
- RawConfig.RawRouting
- Is Undefined Forbidden
- Yes
- Is Null Forbidden
- Yes
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,
HTTP_Methods
} from "@yamato-daiwa/backend";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/",
async handler(request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
initialize
- Generic technical name
- rawConfiguration
- Type
- Server.RawConfiguration
- IP_Address
- Type
- string
- Is Undefined Forbidden
- Yes
- Is Null Forbidden
- Yes
- HTTP
- Type
- RawConfig.HTTP
- Is Undefined Forbidden
- No
- Is Null Forbidden
- Yes
- HTTPS
- Type
- RawConfig.HTTPS
- Is Undefined Forbidden
- No
- Is Null Forbidden
- Yes
- routing
- Type
- RawConfig.RawRouting
- Is Undefined Forbidden
- Yes
- Is Null Forbidden
- Yes
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,
HTTP_Methods
} from "@yamato-daiwa/backend";
const serverApplication: Server = Server.initialize({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
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
start
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,
HTTP_Methods
} from "@yamato-daiwa/backend";
const serverApplication: Server = Server.initialize({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
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
- Type
- string
- Is Undefined Forbidden
- Yes
- Is Null Forbidden
- Yes
- HTTP
- Type
- RawConfig.HTTP
- Is Undefined Forbidden
- No
- Is Null Forbidden
- Yes
- HTTPS
- Type
- RawConfig.HTTPS
- Is Undefined Forbidden
- No
- Is Null Forbidden
- Yes
- routing
- Type
- RawConfig.RawRouting
- Is Undefined Forbidden
- Yes
- Is Null Forbidden
- Yes
Has an object type designed such as all that engineer must comprehend before starting the server application has been made required properties.
IP_Address
- Type
- string
- Is Undefined Forbidden
- Yes
- Is Null Forbidden
- Yes
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,
HTTP_Methods
} from "@yamato-daiwa/backend";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/",
async handler(request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
HTTP
- Type
- RawConfig.HTTP
- Is Undefined Forbidden
- No
- Is Null Forbidden
- Yes
- port
- Type
- number
- Required
- Yes
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,
HTTP_Methods
} from "@yamato-daiwa/backend";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/",
async handler(request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Hello, world!</h1>" });
}
}
]
});
HTTPS
- Type
- RawConfig.HTTPS
- Is Undefined Forbidden
- No
- Is Null Forbidden
- Yes
- port
- Type
- number
- Required
- Yes
- SSL_KeyFileRelativeOrAbsolutePath
- Type
- string
- Required if
- "SSL_Key" property not specified
- SSL_Key
- Type
- string
- Required
- No
- SSL_CertificateFileRelativeOrAbsolutePath
- Type
- string
- Required if
- The "SSL_Certificate" property not specified
- SSL_Certificate
- Type
- string
- Required
- No
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,
HTTP_Methods
} from "@yamato-daiwa/backend";
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: [
{
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,
HTTP_Methods
} from "@yamato-daiwa/backend";
import { secrets as dockerSecrets } from "docker-secret";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTPS: {
port: ProtocolDependentDefaultPorts.HTTPS,
SSL_Key: dockerSecrets.SSL_KEY,
SSL_Certificate: dockerSecrets.SSL_CERTIFICATE
},
routing: [
{
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.
routing
- Type
- RawConfig.RawRouting
- Is Undefined Forbidden
- Yes
- Is Null Forbidden
- Yes
Indexed array, each element of which must have one of the following two types:
- Any class, inhering the
Controller
one, herewith класс itself, not its instance, because the instantiating will be executed by YDB. - The object, where the route and its handler specified.
It is possible to specify the elements of any of these types, but as routes number become large it is recommended to organize most of routes by controller classes.
The following routes definition methods are equivalent. Although the example in the first tab with the object-oriented style seems to be more bulky that the example in the first tab with the functional style, in practical applications such simple request handlers are few while for the more complicated ones the object-oriented programming capabilities are more demanded.
import {
Server,
Request,
Response,
Controller,
ProtocolDependentDefaultPorts,
HTTP_Methods
} from "@yamato-daiwa/backend";
class TopPageController extends Controller {
@Controller.RouteHandler({
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/"
})
public async renderTopPage(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Top Page</h1>" });
}
}
class ProductController extends Controller {
@Controller.RouteHandler({
HTTP_Method: HTTP_Methods.get,
pathTemplate: "products"
})
public async generateProductsPage(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Products list</h1>" });
}
}
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
TopPageController,
ProductController
]
});
import {
Server,
Request,
Response,
ProtocolDependentDefaultPorts,
HTTP_Methods
} from "@yamato-daiwa/backend";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/",
async handler(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Top Page</h1>" });
}
},
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/products",
async handler(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({ HTML_Content: "<h1>Products</h1>" });
}
}
]
});