支持 vhosts 配置参数

This commit is contained in:
陆柯 2019-10-25 14:05:03 +08:00
parent c3fc3e2c88
commit 3b5ef883f1
2 changed files with 25 additions and 3 deletions

View File

@ -19,4 +19,8 @@ module.exports={
password: '', // 登录用户密码 password: '', // 登录用户密码
key: `${process.env.HOME}/.ssh/id_rsa`, // 登录用户私钥文件 key: `${process.env.HOME}/.ssh/id_rsa`, // 登录用户私钥文件
}, },
vhosts: [
{ webroot: 'dist', webindex: 'index.html', domainList: ['']}
],
} }

View File

@ -79,7 +79,7 @@ async function init(){ /*** 设置全局对象 ***/
store: require('greenlock-store-fs'), store: require('greenlock-store-fs'),
email: 'ssl@faronear.org', email: 'ssl@faronear.org',
approvedDomains: wo.Config.sslDomainList, approvedDomains: wo.Config.sslDomainList,
configDir: path.resolve(__dirname, 'ssl'), configDir: path.resolve(process.cwd(), 'ssl'),
app: server, app: server,
}) : null }) : null
@ -91,12 +91,30 @@ async function init(){ /*** 设置全局对象 ***/
server.use(require('compression')()) server.use(require('compression')())
/*** 路由 ***/ /*** 路由 ***/
// vhost 匹配了域名就执行不匹配就next()
// express.static 找到了具体文件就返回找不到就next()
// 所以,如果 vhost匹配了域名且static找到了文件就结束了。如果 vhost 匹配了域名但static找不到文件就继续往下。
if (!wo.Config.vhosts) {
server.use(require('express').static(path.join(process.cwd(), wo.Config.dist), {index: 'index.html'})) server.use(require('express').static(path.join(process.cwd(), wo.Config.dist), {index: 'index.html'}))
// server.use(require('serve-favicon')(path.join(__dirname, 'public', 'favicon.ico'))) // uncomment after placing your favicon in /public // server.use(require('serve-favicon')(path.join(process.cwd(), 'public', 'favicon.ico'))) // uncomment after placing your favicon in /public
}else {
let vhost = require('vhost')
for (let h of wo.Config.vhosts) {
for (let domain of h.domainList) {
server.use(vhost(domain, express.static(path.join(process.cwd(), h.webroot), {index: h.webindex})))
}
}
}
/*** 路由 ***/
//var router = express.Router()
//router.get('/path', function(req,res) { res.redirect('target') })
//server.use(router)
let ipv4 = getMyIp() let ipv4 = getMyIp()
/*** 启动 Web 服务 ***/ /*** 启动 Web 服务 ***/
let webServer
if ('http' === wo.Config.protocol) { if ('http' === wo.Config.protocol) {
webServer = require('http').createServer(server).listen(wo.Config.port, function (err) { webServer = require('http').createServer(server).listen(wo.Config.port, function (err) {
if (err) console.log(err) if (err) console.log(err)