完成模板引擎的安装
parent
6aca3e2695
commit
e6af5e6b1f
|
@ -40,6 +40,11 @@ export default {
|
||||||
name: 'no-reply',
|
name: 'no-reply',
|
||||||
passwd: ''
|
passwd: ''
|
||||||
},
|
},
|
||||||
|
views: {
|
||||||
|
enabled: true,
|
||||||
|
dir: '',
|
||||||
|
ext: '.htm'
|
||||||
|
},
|
||||||
cors: {
|
cors: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
credentials: false,
|
credentials: false,
|
||||||
|
|
17
index.js
17
index.js
|
@ -12,13 +12,12 @@ import fs from 'iofs'
|
||||||
|
|
||||||
import Request from '@gm5/request'
|
import Request from '@gm5/request'
|
||||||
import Response from '@gm5/response'
|
import Response from '@gm5/response'
|
||||||
|
import Views from '@gm5/views'
|
||||||
import { sessionPackage, sessionConnect } from '@gm5/session'
|
import { sessionPackage, sessionConnect } from '@gm5/session'
|
||||||
import { jwtPackage, jwtConnect } from '@gm5/jwt'
|
import { jwtPackage, jwtConnect } from '@gm5/jwt'
|
||||||
|
|
||||||
import config from './config/index.js'
|
import config from './config/index.js'
|
||||||
|
|
||||||
import Views from './lib/views.js'
|
|
||||||
|
|
||||||
import Routers from './middleware/router.js'
|
import Routers from './middleware/router.js'
|
||||||
import Cors from './middleware/cors.js'
|
import Cors from './middleware/cors.js'
|
||||||
|
|
||||||
|
@ -27,7 +26,7 @@ function hideProperty(host, name, value) {
|
||||||
value: value,
|
value: value,
|
||||||
writable: true,
|
writable: true,
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
configurable: true,
|
configurable: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,10 +43,8 @@ export default class Five {
|
||||||
session.domain = session.domain || domain
|
session.domain = session.domain || domain
|
||||||
this.set({ domain, session })
|
this.set({ domain, session })
|
||||||
|
|
||||||
// 用户没手动安装模板引擎时, 才会安装内置的伪引擎
|
// 安装模板引擎
|
||||||
if (!this.$$views) {
|
|
||||||
this.install(Views)
|
this.install(Views)
|
||||||
}
|
|
||||||
|
|
||||||
// 将jwt & session中间件提到最前
|
// 将jwt & session中间件提到最前
|
||||||
// 以便用户自定义的中间件可以直接操作session
|
// 以便用户自定义的中间件可以直接操作session
|
||||||
|
@ -116,7 +113,7 @@ export default class Five {
|
||||||
var list = fs.ls(dir)
|
var list = fs.ls(dir)
|
||||||
|
|
||||||
if (list) {
|
if (list) {
|
||||||
list.forEach((item) => {
|
list.forEach(item => {
|
||||||
var { name } = path.parse(item)
|
var { name } = path.parse(item)
|
||||||
if (name.startsWith('.')) {
|
if (name.startsWith('.')) {
|
||||||
return
|
return
|
||||||
|
@ -127,7 +124,7 @@ export default class Five {
|
||||||
item = path.join(item, './index.js')
|
item = path.join(item, './index.js')
|
||||||
}
|
}
|
||||||
|
|
||||||
this.__MODULES__[name] = import(item).catch((err) => {
|
this.__MODULES__[name] = import(item).catch(err => {
|
||||||
return { default: null }
|
return { default: null }
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -143,7 +140,7 @@ export default class Five {
|
||||||
|
|
||||||
this.__main__()
|
this.__main__()
|
||||||
|
|
||||||
server = http.createServer(function (req, res) {
|
server = http.createServer(function(req, res) {
|
||||||
var request = new Request(req, res)
|
var request = new Request(req, res)
|
||||||
var response = new Response(req, res)
|
var response = new Response(req, res)
|
||||||
|
|
||||||
|
@ -158,7 +155,7 @@ export default class Five {
|
||||||
|
|
||||||
if (fn) {
|
if (fn) {
|
||||||
;(async function next() {
|
;(async function next() {
|
||||||
await fn.call(_this, request, response, function () {
|
await fn.call(_this, request, response, function() {
|
||||||
fn = middleware.shift()
|
fn = middleware.shift()
|
||||||
if (fn) {
|
if (fn) {
|
||||||
next()
|
next()
|
||||||
|
|
29
lib/views.js
29
lib/views.js
|
@ -1,29 +0,0 @@
|
||||||
/**
|
|
||||||
* 简单的模板渲染, 用于在不需要smarty这种重量级模板引擎的时候
|
|
||||||
* 可以兼容smarty的api
|
|
||||||
* @author yutent<yutent.io@gmail.com>
|
|
||||||
* @date 2020/09/24 16:41:31
|
|
||||||
*/
|
|
||||||
|
|
||||||
import fs from 'iofs'
|
|
||||||
import path from 'path'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: 'views',
|
|
||||||
install() {
|
|
||||||
//
|
|
||||||
var updir = this.get('VIEWS')
|
|
||||||
|
|
||||||
return {
|
|
||||||
assign() {
|
|
||||||
//
|
|
||||||
},
|
|
||||||
|
|
||||||
render(file, noParse) {
|
|
||||||
var filePath = path.join(updir, file)
|
|
||||||
var buf = fs.cat(filePath)
|
|
||||||
return Promise.resolve(buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,6 +11,7 @@
|
||||||
"@gm5/response": "^1.3.1",
|
"@gm5/response": "^1.3.1",
|
||||||
"@gm5/controller": "^1.0.0",
|
"@gm5/controller": "^1.0.0",
|
||||||
"@gm5/jwt": "^1.1.0",
|
"@gm5/jwt": "^1.1.0",
|
||||||
|
"@gm5/views": "^1.0.0",
|
||||||
"crypto.js": "^2.0.2",
|
"crypto.js": "^2.0.2",
|
||||||
"es.shim": "^2.0.1",
|
"es.shim": "^2.0.1",
|
||||||
"iofs": "^1.5.0"
|
"iofs": "^1.5.0"
|
||||||
|
|
Loading…
Reference in New Issue