From 48209541257508f0648a3b52d9eaf3243699cf58 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 10 Oct 2022 19:32:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/dev.js | 20 ++++++++++++++------ package.json | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) 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" },