Files
vic-team-vue/server.js

122 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const fs = require('fs')
const path = require('path')
function config(){
const commander = require('commander')
const deepmerge = require('deepmerge')
var Config={}
// 读取配置文件
try {
if (fs.existsSync('./ConfigBasic.js')) {
Config=require('./ConfigBasic.js')
console.info('ConfigBasic loaded')
}
if (fs.existsSync('./ConfigCustom.js')) { // 如果存在,覆盖掉 ConfigBasic 里的默认参数
Config=deepmerge(Config, require('./ConfigCustom.js')) // 注意objectMerge后产生了一个新的对象而不是在原来的Config里添加
console.info('ConfigCustom loaded')
}
if (fs.existsSync('./ConfigSecret.js')) { // 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
Config=deepmerge(Config, require('./ConfigSecret.js'))
console.info('ConfigSecret loaded')
}
}catch(err){
console.error('Loading config files failed: '+err.message)
}
// 载入命令行参数
commander
.version(Config.VERSION, '-v, --version') // 默认是 -V。如果要 -v就要加 '-v --version'
.option('-H, --host <host>', 'Host ip or domain name. Default to ' + Config.host)
.option('-P, --protocol <protocol>', 'Web server protocol http|https|httpall|http2https. Default to ' + Config.protocol)
.option('-p, --port <port>', `Server port. Default to ${Config.port?Config.port:'80|443'}`)
.option('--sslCert <cert>', 'SSL cert file. Default to ' + Config.sslCert)
.option('--sslKey <key>', 'SSL privkey file. Default to ' + Config.sslKey)
.option('--sslCA <ca>', 'SSL ca bundle file')
.parse(process.argv)
// 把命令行参数 合并入配置。
Config.host=commander.host || Config.host
Config.protocol=commander.protocol || Config.protocol // 默认同时启用 http 和 https
Config.port=parseInt(commander.port) || parseInt(Config.port) || (Config.protocol==='http'?80:Config.protocol==='https'?443:undefined) // 端口默认为 http:80, https:443, httpall: 80|443
Config.sslCert=commander.sslCert || Config.sslCert
Config.sslKey=commander.sslKey || Config.sslKey
Config.sslCA=commander.sslCA || Config.sslCA
console.info('Configuration is ready.')
return Config
}
async function init(){ /*** 设置全局对象 ***/
global.wo={} // wo 代表 world或是当前的命名空间把各种类都放在这里防止和其他库的冲突。
wo.Config=config() // 依次载入系统默认配置、用户配置文件、命令行参数
}
(async function start(){
await init()
var express = require('express')
var favicon = require('serve-favicon')
var logger = require('morgan')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
const compression = require('compression')
var server = express()
/*** 通用中间件 ***/
// uncomment after placing your favicon in /public
//server.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
server.use(logger('development'===server.get('env')?'dev':'combined'))
server.use(bodyParser.json())
server.use(bodyParser.urlencoded({ extended: false }))
server.use(cookieParser())
server.use(compression())
/*** 路由 ***/
server.use(express.static(path.join(__dirname,'dist'), {index:'index.html'}))
// var vhost = require('vhost')
// var webroot=express.static(path.join(__dirname,'dist'), {index:'index.html'})
// server.use(vhost('bittic.org', webroot))
// server.use(vhost('www.bittic.org', webroot))
// server.use(vhost('*', webroot))
// server.use(vhost('*.*', webroot))
// server.use(vhost('*.*.*', webroot))
// server.use(vhost('*.*.*.*', webroot))
/*** 启动 Web 服务 ***/
if ('http'===wo.Config.protocol) {
require('http').createServer(server).listen(wo.Config.port, function(err) {
console.log('Server listening on %s://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, wo.Config.port, server.settings.env)
})
}else if ('https'===wo.Config.protocol) {
require('https').createServer({
key: fs.readFileSync(wo.Config.sslKey), cert: fs.readFileSync(wo.Config.sslCert) // , ca: [ fs.readFileSync(wo.Config.sslCA) ] // only for self-signed certificate: https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
}, server).listen(wo.Config.port, function(err){
console.log('Server listening on %s://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, wo.Config.port, server.settings.env)
})
}else if ('httpall'===wo.Config.protocol) {
let portHttp=wo.Config.port?wo.Config.port:80 // 如果port参数已设置使用它否则默认为80
require('http').createServer(server).listen(portHttp, function(err) {
console.log('Server listening on [%s] http://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, portHttp, server.settings.env)
})
let portHttps=(wo.Config.port && wo.Config.port!==80)?wo.Config.port+443:443 // 如果port参数已设置使用它+443否则默认为443
require('https').createServer({
key: fs.readFileSync(wo.Config.sslKey), cert: fs.readFileSync(wo.Config.sslCert) // , ca: [ fs.readFileSync(wo.Config.sslCA) ] // https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
}, server).listen(portHttps, function(err){
console.log('Server listening on [%s] https://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, portHttps, server.settings.env)
})
}else if ('http2https'===wo.Config.protocol) {
wo.Config.port = wo.Config.port || 80
require('http').createServer(express().all('*', function(ask, reply){ /* 错误的API调用进入这里。*/
reply.redirect(`https://${wo.Config.host}`)
})).listen(wo.Config.port, function(err){
console.log('Server listening on [%s] http://%s:%d for %s environment', wo.Config.protocol, wo.Config.host, wo.Config.port, server.settings.env)
})
}
})()