修复文件加载

pull/1/head
yutent 2022-10-10 19:32:10 +08:00
parent e9d50f3009
commit 4820954125
2 changed files with 15 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import MIME_TYPES from './mime-tpyes.js'
import { COMMON_HEADERS } from './constants.js'
const decode = decodeURIComponent
const noc = Buffer.from('')
export default function createServer(root = '', conf = {}) {
const IS_MPA = Object.keys(conf.pages).length > 1
@ -135,7 +136,9 @@ export default function createServer(root = '', conf = {}) {
'</head>',
"<script>window.process = {env: {NODE_ENV: 'development'}}</script></head>"
)
.replace('{{title}}', conf.pages[pageName].title)
.replace('{{title}}', conf.pages[pageName].title || '')
.replace('{{keywords}}', conf.pages[pageName].keywords || '')
.replace('{{description}}', conf.pages[pageName].description || '')
.replace('{{importmap}}', JSON.stringify({ imports: conf.imports }))
.replace(
'<script src="main.js"></script>',
@ -150,12 +153,11 @@ export default function createServer(root = '', conf = {}) {
let rpath = pathname.replace(/^assets\/js\//, '')
let file
if (rpath.startsWith(currentPage)) {
if (IS_MPA) {
file = join(pagesDir, rpath)
} else {
file = join(root, rpath)
}
if (!fs.isfile(file)) {
file = file.replace(/\.vue$/, '/index.vue')
}
@ -206,16 +208,22 @@ export default function createServer(root = '', conf = {}) {
case 'bmp':
res.setHeader('content-type', MIME_TYPES[ext])
code = fs.cat(join(root, pathname))
if (code === null) {
console.error(pathnam, '文件不存在')
res.writeHead(404, 'Not Found')
res.end('')
return
}
break
default:
res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.other)
res.setHeader('content-type', MIME_TYPES[ext] || MIME_TYPES.html)
break
}
res.setHeader('content-length', Buffer.byteLength(code))
res.setHeader('content-length', Buffer.byteLength(code || noc))
res.writeHead(200, 'OK')
res.end(code)
res.end(code || noc)
}
})
.listen(conf.port)

View File

@ -1,7 +1,7 @@
{
"name": "@bytedo/vue-live",
"type": "module",
"version": "0.0.2",
"version": "0.0.3",
"bin": {
"vue-live": "index.js"
},