diff --git a/index.js b/index.js index e742ce0..bd523ff 100644 --- a/index.js +++ b/index.js @@ -6,31 +6,30 @@ */ import 'es.shim' // 加载拓展方法 -import init from './lib/reg-init' +import init from './lib/reg-init.js' import http from 'http' import path from 'path' -import Request from 'http.request' -import Response from 'http.response' +import Request from '../request/index.js' +import Response from '../response/index.js' -import Smarty from 'smartyx' //模板引擎 -import Log from './lib/module/log' //基础日志记录工具 -import Email from './lib/module/sendmail' //加载email发送类 +// import Smarty from 'smartyx' //模板引擎 +import Log from './module/log.js' //基础日志记录工具 +import Email from './module/sendmail.js' //加载email发送类 import Mysql from 'mysqli' //加载mysql操作类 import Ioredis from 'ioredis' import sec from 'crypto.js' -import path from 'path' import url from 'url' import fs from 'iofs' import child from 'child_process' -import Controller from './lib/controller' +import Controller from './lib/controller.js' -import routerWare from './lib/middleware/router' -import cookieWare from './lib/middleware/cookie' -import sessionWare from './lib/middleware/session' -import credentialsWare from './lib/middleware/credentials' +import routerWare from './middleware/router.js' +import cookieWare from './middleware/cookie.js' +import sessionWare from './middleware/session.js' +import credentialsWare from './middleware/credentials.js' var log = console.log @@ -49,22 +48,6 @@ export default class Five { hideProperty(this, '__MODULES__', { __error__: null }) hideProperty(this, '__MIDDLEWARE__', []) hideProperty(this, '__INSTANCE__', {}) - - global.libs = { - Smarty, //模板引擎 - Log, //基础日志记录工具 - Email, //加载email发送类 - Mysql, //加载mysql操作类 - Ioredis - } - global.Util = { - sec, - path, - url, - fs, - child - } - global.Controller = Controller } __init__() { @@ -147,7 +130,7 @@ export default class Five { } // 预加载应用 preload(dir) { - var list = Util.fs.ls(dir) + var list = fs.ls(dir) if (list) { list.forEach(file => { diff --git a/lib/controller.js b/lib/controller.js index f12b372..031f5b8 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -5,10 +5,11 @@ * */ -'use strict' +import Smarty from 'smartyx' //模板引擎 -const smarty = new libs.Smarty() -const jwt = require('./module/jwt') +import { sign, verify } from '../module/jwt.js' + +const smarty = new Smarty() export default class Controller { constructor({ ctx, req, res }) { @@ -18,8 +19,8 @@ export default class Controller { this.response = res this.jwt = { - sign: jwt.sign.bind(this), - result: jwt.verify.call(this) + sign: sign.bind(this), + result: verify.call(this) } smarty.config('path', this.ctx.get('VIEWS')) diff --git a/lib/middleware/router.js b/lib/middleware/router.js deleted file mode 100644 index 1ead7ae..0000000 --- a/lib/middleware/router.js +++ /dev/null @@ -1,53 +0,0 @@ -/** - * 路由 - * @authors yutent (yutent@doui.cc) - * @date 2015-10-01 19:11:19 - * - */ -'use strict' - -export default function(req, res, next) { - if (!this.__MODULES__[req.app]) { - if (!this.__MODULES__.__error__) { - res.error(`The app [${req.app}] not found`, 404) - } else { - res.error( - this.get('debug') - ? this.__MODULES__.__error__.stack - : this.__MODULES__.__error__, - 500 - ) - } - return - } - - try { - if (req.path.length < 1) { - req.path.push('index') - } - - var app = new this.__MODULES__[req.app]({ ctx: this, req, res }) - - if (this.get('routeMode') === 1) { - var act = req.path.shift() - - if (app[act + 'Action']) { - app[act + 'Action'].apply(app, req.path).catch(err => { - res.error(this.get('debug') ? err.stack || err : err, 500) - }) - } else { - res.error(`Action[${act}] not found`, 404) - } - } else { - if (app.indexAction) { - app.indexAction.apply(app, req.path).catch(err => { - res.error(this.get('debug') ? err.stack || err : err, 500) - }) - } else { - res.error(`Default Action not found`, 404) - } - } - } catch (err) { - res.error(this.get('debug') ? err.stack || err : err, 500) - } -} diff --git a/lib/middleware/cookie.js b/middleware/cookie.js similarity index 100% rename from lib/middleware/cookie.js rename to middleware/cookie.js diff --git a/lib/middleware/credentials.js b/middleware/credentials.js similarity index 100% rename from lib/middleware/credentials.js rename to middleware/credentials.js diff --git a/middleware/router.js b/middleware/router.js new file mode 100644 index 0000000..ef27b60 --- /dev/null +++ b/middleware/router.js @@ -0,0 +1,55 @@ +/** + * 路由 + * @authors yutent (yutent@doui.cc) + * @date 2015-10-01 19:11:19 + * + */ +'use strict' + +export default function(req, res, next) { + if (!this.__MODULES__[req.app]) { + if (!this.__MODULES__.__error__) { + res.error(`The app [${req.app}] not found`, 404) + } else { + res.error( + this.get('debug') + ? this.__MODULES__.__error__.stack + : this.__MODULES__.__error__, + 500 + ) + } + return + } + + try { + if (req.path.length < 1) { + req.path.push('index') + } + + this.__MODULES__[req.app].then(Mod => { + var app = new Mod({ ctx: this, req, res }) + + if (this.get('routeMode') === 1) { + var act = req.path.shift() + + if (app[act + 'Action']) { + app[act + 'Action'].apply(app, req.path).catch(err => { + res.error(this.get('debug') ? err.stack || err : err, 500) + }) + } else { + res.error(`Action[${act}] not found`, 404) + } + } else { + if (app.indexAction) { + app.indexAction.apply(app, req.path).catch(err => { + res.error(this.get('debug') ? err.stack || err : err, 500) + }) + } else { + res.error(`Default Action not found`, 404) + } + } + }) + } catch (err) { + res.error(this.get('debug') ? err.stack || err : err, 500) + } +} diff --git a/lib/middleware/session.js b/middleware/session.js similarity index 92% rename from lib/middleware/session.js rename to middleware/session.js index 22d6fdf..f1b5641 100644 --- a/lib/middleware/session.js +++ b/middleware/session.js @@ -4,8 +4,8 @@ * @date 2018-07-26 15:50:25 * @version $Id$ */ -import redisStore from '../module/redis-store' -import nativeStore from '../module/native-store' +import redisStore from '../module/redis-store.js' +import nativeStore from '../module/native-store.js' export default function(req, res, next) { var opt = this.get('session') diff --git a/lib/module/jwt.js b/module/jwt.js similarity index 100% rename from lib/module/jwt.js rename to module/jwt.js diff --git a/lib/module/log.js b/module/log.js similarity index 100% rename from lib/module/log.js rename to module/log.js diff --git a/lib/module/native-store.js b/module/native-store.js similarity index 100% rename from lib/module/native-store.js rename to module/native-store.js diff --git a/lib/module/redis-store.js b/module/redis-store.js similarity index 100% rename from lib/module/redis-store.js rename to module/redis-store.js diff --git a/lib/module/sendmail.js b/module/sendmail.js similarity index 96% rename from lib/module/sendmail.js rename to module/sendmail.js index b851e3c..3209c0a 100644 --- a/lib/module/sendmail.js +++ b/module/sendmail.js @@ -6,7 +6,8 @@ */ 'use strict' -const mailx = require('mailx') +import mailx from 'mailx' + export default class Sendmail { constructor({ host, port, mail, passwd } = {}) { if (!host || !port || !mail || !passwd) { diff --git a/package.json b/package.json index ae54601..1b088b5 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "node-five", + "name": "@gm5/core", "version": "3.2.6", "type": "module", "description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手", @@ -10,12 +10,11 @@ "@bytedo/es.shim": "^1.0.0", "iofs": "^1.3.2", "mysqli": "^3.0.11", - "http.request": "^1.1.0", - "http.response": "^1.0.2", - "http.cookie": "^1.0.2", + "@gm5/request": "^1.0.0", + "@gm5/response": "^1.0.0", + "@gm5/cookie": "^1.0.0", "smartyx": "^1.3.1", - "ioredis": "^3.2.2", - "mailx": "^0.0.11" + "ioredis": "^3.2.2" }, "devDependencies": {}, "repository": {