npm

@flowxjs/http
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

HTTP

TypeService 架构之 HTTP 服务插件。

Usage

安装插件

$ npm i @flowxjs/http

日志文件

export type TTypeServiceLogger = Console;
export const TypeServiceLogger = console;

框架文件 src/app.bootstrap.ts

import { Factory, TAnnotationScanerResult, TypeServiceExtendtion, TTypeServiceBootstrap } from '@flowxjs/typeservice';
import { TTypeServiceLogger, TypeServiceLogger } from './app.logger';
import { Http } from './app.http';
import { TProcessArgv } from '@flowxjs/http';

@Factory.Logger(TypeServiceLogger)
@Factory.Setup(Http)
export class ApplicationFactory<
  Z extends TypeServiceExtendtion<TTypeServiceLogger, Z, TProcessArgv> & TTypeServiceBootstrap
> extends TypeServiceExtendtion<TTypeServiceLogger, Z, TProcessArgv> implements TTypeServiceBootstrap {
  message(value: any) {
    if (value instanceof Error) return this.factory.logger.error(value);
    this.factory.logger.info(value);
  }

  initialize(meta: TAnnotationScanerResult) {
    // console.log('factory initialize', meta);
  }

  terminate(meta: TAnnotationScanerResult) {
    // console.log('factory terminate', meta);
  }
}

服务文件 src/app.http.ts

import { TAnnotationScanerResult, TypeServiceExtendtion, TTypeServiceBootstrap } from '@flowxjs/typeservice';
import { HttpServer, THttpDefaultState, THttpDefaultContext, Controllers, Use, TProcessArgv } from '@flowxjs/http';
import { TTypeServiceLogger } from './app.logger';
import { CustomController } from './controller';
import { Hello } from './middleware';

export interface THttpState extends THttpDefaultState {}
export interface THttpContext extends THttpDefaultContext<TTypeServiceLogger> {}

@Controllers(CustomController)
@Use(Hello('  - Middleware from app.http.ts'))
export class Http<
  Z extends TypeServiceExtendtion<TTypeServiceLogger, Z, TProcessArgv> & TTypeServiceBootstrap
> extends HttpServer<TTypeServiceLogger, THttpState, THttpContext, Z> {
  initialize(meta: TAnnotationScanerResult) {
    this.factory.on('http:message').subscribe(msg => this.factory.logger.info(msg));
    return super.initialize(meta);
  }
}

路由文件 src/controller.ts

import { Controller, Get, Use, Middleware, Ip, Url } from '@flowxjs/http';
import { injectable } from 'inversify';
import { Hello } from './middleware';

@injectable()
@Controller()
@Use(Hello('  - Middleware from Controler Class'))
export class CustomController {
  @Get()
  @Middleware(Hello('  - Middleware from Controler Method'))
  hello(
    @Ip() ip: string,
    @Url() url: string
  ) {
    return `<html><body>Hello World. <br />My ip is ${ip}. <br />My Url is ${url}</body></html>`;
  }
}

中间件文件 src/middleware.ts

import * as Koa from 'koa';
import * as compose from 'koa-compose';
import { THttpState, THttpContext } from './app.http';
export function Hello(str: string): compose.Middleware<Koa.ParameterizedContext<THttpState, THttpContext>> {
  return async (ctx, next) => {
    console.log(str);
    await next();
  }
}

启动文件 src/index.ts

import * as parseArgs from 'minimist';
import { TypeServiceFactory } from '@flowxjs/typeservice';
import { ApplicationFactory } from './app.bootstrap';
import { TProcessArgv } from '@flowxjs/http';

const Argv = parseArgs<TProcessArgv>(process.argv.slice(2));
const app = new TypeServiceFactory(ApplicationFactory, Argv);
app.bootstrap();

脚本

  • 启动 ts-node src/index.ts --port=9000

Package Sidebar

Install

npm i @flowxjs/http

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

2.83 MB

Total Files

35

Last publish

Collaborators

  • evio