@yamato-daiwa/backend
v0.4.0
Published
Backend framework with build-in TypeScript type safety. Intended to be used in full-stack applications where both client and server part written in TypeScript.
Maintainers
Readme
Yamato Daiwa Backend 〔YDB〕

Back-end framework with built-in TypeScript type safety. Clean API, no husk. Intended to be used in full-stack applications where both client and server part written in TypeScript.
Installation
npm i @yamato-daiwa/backend -EQuick Examples
:warning: Warning: Below examples has been developed to demonstrate the API of the framework such as easy to understand. For this, the splitting of the code to files and code itself has been minified, but this approach is unfit for the development of the real applications from the viewpoint of architecture.
"Hello, world!"
import {
Server,
Request,
Response,
ProtocolDependentDefaultPorts,
HTTP_Methods
} from "@yamato-daiwa/backend";
/* Running the test:
* tsx EntryPoint.ts OR npm run start
* */
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>" });
}
}
]
});See the "Hello, world!" tutorial for the details.
HTTPS Support
import {
Server,
Request,
Response,
ProtocolDependentDefaultPorts,
HTTP_Methods
} from "@yamato-daiwa/backend";
import Path from "path";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
HTTPS: {
port: ProtocolDependentDefaultPorts.HTTPS,
SSL_CertificateFileRelativeOrAbsolutePath: "SSL/SSL_Certificate.pem",
SSL_KeyFileRelativeOrAbsolutePath: "SSL/SSL_Key.pem",
// Or pass the raw strings:
// 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>" });
}
}
]
});See the HTTPS support tutorial for the details.
Vs. Express
import type { Express as ExpressApplication } from "express";
import type Express from "express";
import createExpressApplication from "express";
import HTTPS from "https";
import FileSystem from "fs";
const expressApplication: ExpressApplication = createExpressApplication();
expressApplication.get(
"/",
(_request: Express.Request, response: Express.Response): void => {
response.send("<h1>Hello, world!</h1>");
}
);
const HTTPS_Server: HTTPS.Server = HTTPS.createServer(
{
key: FileSystem.readFileSync("./SSL/key.pem"),
cert: FileSystem.readFileSync("./SSL/cert.pem")
},
expressApplication
);
HTTPS_Server.listen(443, "127.0.0.1");Vs. Express + routing-controllers
For the Spring of 2024, the HTTPS example was not documented for routing-controllers. Although the HTTPS usage is possible with routing-controllers, the code is pretty verbose:
import Express, { type Express as ExpressApplication } from "express";
import createExpressApplication from "express";
import { useExpressServer as supportClassSyntax } from "routing-controllers";
import HTTPS from "https";
import FileSystem from "fs";
const expressApplication: ExpressApplication = createExpressApplication();
expressApplication.get(
"/",
(_request: Express.Request, response: Express.Response): void => {
response.send("<h1>Hello, world!</h1>");
}
);
const HTTPS_Server: HTTPS.Server = HTTPS.createServer(
{
key: FileSystem.readFileSync("./SSL/key.pem"),
cert: FileSystem.readFileSync("./SSL/cert.pem")
},
expressApplication
);
supportClassSyntax(expressApplication);
HTTPS_Server.listen(443, "127.0.0.1");Routing and Controllers
Minimal Example
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>"
});
}
},
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/checkout",
async handler(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({
HTML_Content: "<h1>Checkout</h1>"
});
}
}
]
});See Functional API section of Routing and Controllers tutorial for details.
Routes with Parameters
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: "products/:PRODUCT_ID",
async handler(request: Request, response: Response): Promise<void> {
const targetProductID: number = request.validateAndProcessRoutePathParameters<{ PRODUCT_ID: number; }>({
PRODUCT_ID: {
preValidationModifications: convertPotentialStringToIntegerIfPossible,
type: Number,
numbersSet: RawObjectDataProcessor.NumbersSets.naturalNumberOrZero,
isUndefinedForbidden: true,
isNullForbidden: true
}
}).PRODUCT_ID;
return response.submitWithSuccess({
HTML_Content: `<h1>Product with ID: ${ targetProductID }</h1>`
});
}
},
// ...
]
});See Defining of Routes with Path Parameters section of Routing and Controllers tutorial for details.
Numeric Parameters
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import {
HTTP_Methods,
convertPotentialStringToNumberIfPossible,
RawObjectDataProcessor
} from "@yamato-daiwa/es-extensions";
Server.initializeAndStart({
IP_Address: "127.0.0.1",
HTTP: { port: ProtocolDependentDefaultPorts.HTTP },
routing: [
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "products/:PRODUCT_ID",
async handler(request: Request, response: Response): Promise<void> {
const targetProductID: number = request.validateAndProcessRoutePathParameters<{ PRODUCT_ID: number; }>({
PRODUCT_ID: {
preValidationModifications: convertPotentialStringToNumberIfPossible,
type: Number,
numbersSet: RawObjectDataProcessor.NumbersSets.naturalNumberOrZero,
isNaN_Forbidden: true,
isUndefinedForbidden: true,
isNullForbidden: true
}
}).PRODUCT_ID;
return response.submitWithSuccess({
HTML_Content: `<h1>Product with ID: ${ targetProductID }</h1>`
});
}
},
// ...
]
});See Numeric Path Parameters section of Routing and Controllers tutorial for details.
Controllers
import { Request, Response, Controller } from "@yamato-daiwa/backend";
import {
HTTP_Methods,
convertPotentialStringToIntegerIfPossible,
RawObjectDataProcessor
} from "@yamato-daiwa/es-extensions";
export default 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>"
});
}
@Controller.RouteHandler({
HTTP_Method: HTTP_Methods.get,
pathTemplate: "products/:PRODUCT_ID"
})
public async generateProductProfilePage(request: Request, response: Response): Promise<void> {
const targetProductID: number = request.validateAndProcessRoutePathParameters<{ PRODUCT_ID: number; }>({
PRODUCT_ID: {
preValidationModifications: convertPotentialStringToIntegerIfPossible,
type: Number,
numbersSet: RawObjectDataProcessor.NumbersSets.naturalNumberOrZero,
isNaN_Forbidden: true,
isUndefinedForbidden: true,
isNullForbidden: true
}
}).PRODUCT_ID;
return response.submitWithSuccess({
HTML_Content: `<h1>Product with ID: ${ targetProductID }</h1>`
});
}
}Entry point example:
import ProductController from "./Controllers/ProductController";
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: [
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/",
async handler(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({
HTML_Content: "<h1>Top Page</h1>"
});
}
},
ProductController,
{
HTTP_Method: HTTP_Methods.get,
pathTemplate: "/checkout",
async handler(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({
HTML_Content: "<h1>Checkout</h1>"
});
}
}
]
});See Controllers section of Routing and Controllers tutorial for details.
Dotenv config
Entry point
import { Server, Request, Response } from "@yamato-daiwa/backend";
import { HTTP_Methods, RawObjectDataProcessor, convertPotentialStringToNumberIfPossible } from "@yamato-daiwa/es-extensions";
import { ObjectDataFilesProcessor } from "@yamato-daiwa/es-extensions-nodejs";
const configFromDotEnvFile: Readonly<{
IP_ADDRESS: string;
HTTP_PORT: number;
}> = ObjectDataFilesProcessor.processFile({
filePath: ".env",
schema: ObjectDataFilesProcessor.SupportedSchemas.DOTENV,
validDataSpecification: {
nameForLogging: "ConfigFromDotenvFile",
subtype: RawObjectDataProcessor.ObjectSubtypes.fixedKeyAndValuePairsObject,
properties: {
IP_ADDRESS: {
type: String,
required: true
},
HTTP_PORT: {
preValidationModifications: convertPotentialStringToNumberIfPossible,
type: Number,
numbersSet: RawObjectDataProcessor.NumbersSets.nonNegativeInteger,
required: true
}
}
}
});
Server.initializeAndStart({
IP_Address: configFromDotEnvFile.IP_ADDRESS,
HTTP: { port: configFromDotEnvFile.HTTP_PORT },
routing: [
{
route: { HTTP_Method: HTTP_Methods.get, pathTemplate: "/" },
async handler(_request: Request, response: Response): Promise<void> {
return response.submitWithSuccess({
HTML_Content: "<h1>Top page</h1>"
});
}
}
]
});Dotenv file
IP_ADDRESS=127.0.0.1
HTTP_PORT=80See the Dotenv configuration tutorial for the details.
Console Line Interface (CLI) configuration
Because the console commands parsing is actual for the console applications, not just for server applications, the ConsoleCommandsParser utility is available in @yamato-daiwa/es-extensions-nodejs package.
import { Server, Request, Response, ProtocolDependentDefaultPorts } from "@yamato-daiwa/backend";
import { HTTP_Methods } from "@yamato-daiwa/es-extensions";
import { ConsoleCommandsParser, ObjectDataFilesProcessor } from "@yamato-daiwa/es-extensions-nodejs";
const configFromConsoleCommand: ConsoleCommandsParser.ParsedCommand<
Readonly<{
IP_Address?: string;
HTTP_Port?: number;
}>
> = ConsoleCommandsParser.parse(
process.argv,
{
applicationName: "Server",
defaultCommand: {
IP_Address: {
type: ConsoleCommandsParser.ParametersTypes.string,
required: false
},
HTTP_Port: {
type: ConsoleCommandsParser.ParametersTypes.number,
numbersSet: RawObjectDataProcessor.NumbersSets.nonNegativeInteger,
required: false
}
}
}
);
Server.initializeAndStart({
IP_Address: configFromConsoleCommand.IP_Address ?? "127.0.0.1",
HTTP: { port: configFromConsoleCommand.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>Top page</h1>"
});
}
}
]
});See the Console Line Interface configuration for the details.
Functionality Tutorials
Please take the tutorials in following order.
