修复文件加载

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

View File

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