Run mion as a Node.js Http Server

Note that a mion server is not a generic http server and does not support common scenarios like file uploads, path params or even query params. Is custom tailored form mion APIs so it can be lightweight and fast.

npm install
npm i @mionkit/http

Start a Node.js Server

init Node
import {NodeHttpOptions, startNodeServer} from '@mionkit/http';
import './myApi.routes';

// init a http server with options specific for node
const nodeOptions: Partial<NodeHttpOptions> = {port: 3000};
startNodeServer(nodeOptions);
./myApi.routes.ts
import {RpcError} from '@mionkit/core';
import {RouterOptions, initMionRouter, headersHook, hook, route} from '@mionkit/router';

export type User = {id: string; name: string; surname: string};

// set options and init router
export const routerOptions: Partial<RouterOptions> = {prefix: 'api/v1'};
export const myApi = initMionRouter(
    // all function parameters will be automatically validated before the function is called
    {
        auth: headersHook('authorization', (ctx, token: string): void => {
            if (!token) throw new RpcError({statusCode: 401, message: 'Not Authorized'});
        }),
        users: {
            sayHello: route((ctx, user: User): string => `Hello ${user.name} ${user.surname}`),
        },
        log: hook((ctx): void => console.log(Date.now(), ctx.path, ctx.response.statusCode), {runOnError: true}),
    },
    routerOptions
);

// Export the type of the Api (used by the client)
export type MyApi = typeof myApi;
Quick Tip:
It is a good idea to split the routes definition and server initialization code so mion can be easily configured to run on multiple environments!

Type Reference

RouterOptions

Basic options to configure mion router. These options are independent of the environment mion is being used (server or serverless).

export interface RouterOptions<Req = any, SharedData = any> extends CoreOptions {
    /** prefix for all routes, i.e: api/v1.
     * path separator is added between the prefix and the route */
    prefix: string;
    /** suffix for all routes, i.e: .json.
     * Not path separators is added between the route and the suffix */
    suffix: string;
    /** Transform the path before finding a route */
    pathTransform?: (request: Req, path: string) => string;
    /** factory function to initialize shared call context data */
    sharedDataFactory?: SharedDataFactory<SharedData>;
    /** enable automatic parameter validation, defaults to true */
    useValidation: boolean;
    /** Enables serialization/deserialization */
    useSerialization: boolean;
    /** Reflection and Deepkit Serialization-Validation options */
    reflectionOptions: ReflectionOptions;
    /** Custom JSON parser, defaults to Native js JSON */
    bodyParser: JsonParser;
    /** Used to return public data structure when adding routes */
    getPublicRoutesData: boolean;
    /** automatically generate and uuid */
    autoGenerateErrorId: boolean;
    /** client routes are initialized by default */
    skipClientRoutes: boolean;
}

NodeHttpOptions

export interface NodeHttpOptions {
    protocol: 'http' | 'https';
    port: number;
    /** Native node's ServerOptions. By default maxHeaderSize defaults to 8KB, same as in latest node versions */
    options: ServerOptions;
    /** Set of default response header to add to every response*/
    defaultResponseHeaders: Record<string, string>;
    /**
     * 256KB by default, same as lambda payload
     * @link https://docs.aws.amazon.com/lambda/latest/operatorguide/payload.html
     * */
    maxBodySize: number; // default 256KB
}