diff --git a/lib/dev.js b/lib/dev.js index c5e9e72..82cfcad 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -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 = {}) { '', "" ) - .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( '', @@ -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) diff --git a/package.json b/package.json index 392350a..3b8d323 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@bytedo/vue-live", "type": "module", - "version": "0.0.2", + "version": "0.0.3", "bin": { "vue-live": "index.js" },