imsdk-server-core
Version:
轻量级Web服务器框架、WebSocket服务器框架。采用Typescript编写,简单易用。
98 lines (97 loc) • 3.55 kB
TypeScript
/// <reference types="node" />
/**
* 对express及其常用插件封装的类
* express相关信息:https://github.com/expressjs/express
* helmet相关信息:https://github.com/helmetjs/helmet
* compression相关信息:https://github.com/expressjs/compression
* cookie-parser相关信息:https://github.com/expressjs/cookie-parser
* body-parser相关信息:https://github.com/expressjs/body-parser
* multer相关信息:https://github.com/expressjs/multer
*/
import express from 'express';
import compression from 'compression';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import multer from 'multer';
import https from 'https';
import http from 'http';
import type { Logger } from 'log4js';
import type { ServeStaticOptions } from 'serve-static';
import type { EnvContext } from './EnvContext';
export interface WebServerConfig {
helmet?: {
[key: string]: any;
};
compress?: compression.CompressionOptions;
cookieSecret?: string | string[];
cookieOptions?: cookieParser.CookieParseOptions;
bodyParserJson?: bodyParser.OptionsJson;
bodyParserForm?: bodyParser.OptionsUrlencoded;
uploadKey?: string;
uploadDir?: string;
uploadMimeTypes?: {
[key: string]: string;
};
webRootUrl?: string;
webSignPwd?: string;
}
export declare class WebServer {
private _context;
private _config;
private _logger;
private _webapp;
private _server;
private _upload;
/**
* @param context 上下文包装类实例
* @param category 日志分类
* @param config 配置信息
*/
constructor(context: EnvContext, category: string, config?: WebServerConfig);
/**
* 加载比较常用的几个第三方模块
*/
loadBaseModules(): void;
/**
* 加载打印全部请求信息的模块
* @param logHeaders 是否打印请求头信息
* @param logArgs 是否打印参数信息
*/
loadPrintModule(logHeaders?: boolean, logArgs?: boolean): void;
/**
* 加载文件上传模块,具体属性参考依赖库 https://github.com/expressjs/multer
* @param url 上传请求的url
* @param cb 上传成功或失败的回调,第一个回调参数code=200为成功,其它为失败
* @param options 加载文件上传模块,具体属性参考依赖库 https://github.com/expressjs/multer
*/
loadUploadModule(url: string, cb: (code: number, data: string | {
[key: string]: any;
}, req: express.Request, resp: express.Response) => void, options?: multer.Options): void;
/**
* 加载静态资源到express容器,具体属性参考依赖库 https://github.com/expressjs/express
* @param url http(s)访问请求路径
* @param path 文件或文件夹的路径
* @param options 配置信息,具体属性参考依赖库 https://github.com/expressjs/express
*/
loadStaticModule(url: string, path: string, options?: ServeStaticOptions): void;
/**
* 开启服务器
* @param callback 服务器启动后的回调函数
*/
start(callback?: () => void): void;
/**
* 关闭服务器
* @param callback 服务器关闭后的回调函数
*/
close(callback?: (error: Error) => void): void;
get logger(): Logger;
get webapp(): express.Express;
get server(): http.Server | https.Server;
get webRootUrl(): string;
get webSignPwd(): string;
/**
* 确保返回一个object类型
* @param config
*/
private ensureObject;
}