优化配置二级目录时, 自动302重定向

pull/1/head
yutent 2023-02-19 18:12:51 +08:00
parent 8f5e70567f
commit b048be1a56
1 changed files with 8 additions and 1 deletions

View File

@ -53,8 +53,9 @@ export default async function createServer(root = '', conf = {}) {
server server
.on('request', function (req, res) { .on('request', function (req, res) {
let prefix = DEPLOY_PATH ? DEPLOY_PATH.replace(/\/$/, '') : ''
let url = let url =
DEPLOY_PATH && req.url.startsWith(DEPLOY_PATH.replace(/\/$/, '')) prefix && req.url.startsWith(prefix)
? req.url.slice(DEPLOY_PATH.length) ? req.url.slice(DEPLOY_PATH.length)
: req.url.slice(1) : req.url.slice(1)
let pathname = parse(url).pathname let pathname = parse(url).pathname
@ -62,6 +63,12 @@ export default async function createServer(root = '', conf = {}) {
isIndex = false // 是否渲染目录页 isIndex = false // 是否渲染目录页
let ext let ext
if (prefix && req.url === '/') {
res.setHeader('Location', DEPLOY_PATH)
res.writeHead(302, 'Redirect')
return res.end('')
}
if (pathname) { if (pathname) {
pathname = pathname.split('/') pathname = pathname.split('/')