增加https支持
parent
f5f0cbf1bc
commit
d3245686be
35
lib/dev.js
35
lib/dev.js
|
@ -1,4 +1,5 @@
|
|||
import http from 'http'
|
||||
import https from 'https'
|
||||
import fs from 'iofs'
|
||||
import { join, resolve, dirname } from 'path'
|
||||
import { parse } from 'url'
|
||||
|
@ -9,9 +10,14 @@ import MIME_TYPES from './mime-tpyes.js'
|
|||
import { COMMON_HEADERS } from './constants.js'
|
||||
|
||||
const noc = Buffer.from('')
|
||||
const SERVER_OPTIONS = {}
|
||||
|
||||
export default function createServer(root = '', conf = {}) {
|
||||
const IS_MPA = Object.keys(conf.pages).length > 1
|
||||
const PORT = conf.devServer.port
|
||||
const USE_HTTPS = conf.devServer.https
|
||||
const DOMAIN = conf.devServer.domain || 'localhost'
|
||||
const WEB_SERVER = USE_HTTPS ? https : http
|
||||
let indexPage = Object.keys(conf.pages)
|
||||
.map(it => {
|
||||
let tmp = it + '.html'
|
||||
|
@ -22,8 +28,16 @@ export default function createServer(root = '', conf = {}) {
|
|||
let pagesDir = '',
|
||||
currentPage = ''
|
||||
|
||||
http
|
||||
.createServer(function (req, res) {
|
||||
if (USE_HTTPS) {
|
||||
Object.assign(SERVER_OPTIONS, conf.devServer.ssl)
|
||||
|
||||
if (!SERVER_OPTIONS.key || !SERVER_OPTIONS.cert) {
|
||||
console.error('证书错误: https 证书不能为空!!!\n')
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
WEB_SERVER.createServer(SERVER_OPTIONS, function (req, res) {
|
||||
let pathname = parse(req.url.slice(1)).pathname
|
||||
let pageName = '',
|
||||
isIndex = false
|
||||
|
@ -64,11 +78,6 @@ export default function createServer(root = '', conf = {}) {
|
|||
|
||||
let code = ''
|
||||
|
||||
// return res.end(JSON.stringify({url: req.url, ext, pageName}))
|
||||
|
||||
res.setHeader('x-ext', ext)
|
||||
res.setHeader('x-page', pageName)
|
||||
|
||||
switch (ext) {
|
||||
case 'html':
|
||||
{
|
||||
|
@ -163,13 +172,15 @@ export default function createServer(root = '', conf = {}) {
|
|||
res.end(code || noc)
|
||||
}
|
||||
})
|
||||
.listen(conf.port)
|
||||
.listen(PORT)
|
||||
.on('error', err => {
|
||||
console.log(`${conf.port}端口被占用~~~`)
|
||||
conf.port++
|
||||
createServer()
|
||||
console.log(`${PORT}端口被占用~~~`)
|
||||
conf.devServer.port++
|
||||
createServer(root, conf)
|
||||
})
|
||||
.on('listening', _ => {
|
||||
console.log('启动成功, 请访问', 'http://127.0.0.1:' + conf.port)
|
||||
console.log('启动成功, 可通过以下地址访问')
|
||||
console.log(' 本地: %s://%s:%d', USE_HTTPS ? 'https' : 'http', '127.0.0.1', PORT)
|
||||
console.log(' 外网: %s://%s:%d\n', USE_HTTPS ? 'https' : 'http', DOMAIN, PORT)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "@bytedo/vue-live",
|
||||
"type": "module",
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.7",
|
||||
"bin": {
|
||||
"vue-live": "index.js"
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue