mm_os
Version:
MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。
68 lines (61 loc) • 1.33 kB
JavaScript
const Koa = require('koa');
const { Adapter } = require('./adapter');
/**
* Web适配器类
* 基于Koa框架的HTTP服务器适配器
*/
class Web extends Adapter {
static config = {
host: '0.0.0.0',
port: 8000
};
/**
* 构造函数
* @param {object} config 配置参数
* @param {object} parent 父对象
*/
constructor(config, parent) {
super({ ...Web.config, ...config }, parent);
}
}
/**
* 预置
*/
Web.prototype._preset = function () {
this.web = new Koa(this.config);
// 设置cookie签名密钥
this.web.keys = ['mos-secret-key-2024', 'mos-backup-key-2024'];
};
/**
* 使用中间件
* @param {Function} middleware 中间件函数
*/
Web.prototype.use = function (middleware) {
this.web.use(middleware);
};
/**
* 初始化
* @param {object} server 服务器实例
* @returns {object} 适配器实例
*/
Web.prototype.init = function (server) {
if (server) {
this.getServer = function () {
return server;
};
}
return this;
};
/**
* 启动
*/
Web.prototype.start = function () {
var {
host,
port
} = this.config;
this.web.listen(port, host, () => {
this.log('info', `web启动成功!访问网址:http://${host}:${port}`);
});
};
exports.Web = Web;