跳至內容
Astera Technology

應用掛在子路徑,資產基準也要核

掛載及根目錄參考,各自解一次。

下載完整個案資料及參考答案 ↓

訂判斷及所需輸入

本例掛在 /staff/。相對 app.js 沿該基準才到 /staff/app.js;網站根基準得 /app.js,已在外。明確 /app.js 即使改對基準,也仍在外。

已填本地測試工作表
情況輸入 JSON
原例測試{"mount":"/staff/","reference":"app.js"}
下載可編輯 CSV ↓

跑所附比較

真正網址解析只用假設 .invalid 標記,不發請求;解到地址未證明主機有提供。

錯誤及所需輸出
路徑輸出 JSON
錯誤路徑{"pathname":"/app.js","insideMount":false}
所需路徑{"pathname":"/staff/app.js","insideMount":true}
下載可編輯 CSV ↓
完整本地 Node24.14.1 程序
import assert from 'node:assert/strict';

const run=function run(i,corrected=true){
    if(i.mount===null)return {held:true};
    const resolved=new URL(i.reference,'https://local.example.invalid'+(corrected?i.mount:'/'));
    return {pathname:resolved.pathname,insideMount:resolved.pathname.startsWith(i.mount)};
  };
const cases=[{"input":{"mount":"/staff/","reference":"app.js"},"corrected":false,"expected":{"pathname":"/app.js","insideMount":false}},{"input":{"mount":"/staff/","reference":"app.js"},"corrected":true,"expected":{"pathname":"/staff/app.js","insideMount":true}},{"input":{"mount":"/staff/","reference":"/app.js"},"corrected":true,"expected":{"pathname":"/app.js","insideMount":false}},{"input":{"mount":null,"reference":"app.js"},"corrected":true,"expected":{"held":true}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
下載可執行例子 ↓

按邊界依據作判斷

根目錄參考邊界說明,改一個基準不能修所有字樣;掛載欠缺便停。

  1. 取明訂掛載位置
  2. 按該基準解析
  3. 核最後路徑

路徑解析不能確認真實主機服務。

可編輯邊界輸入及結果
條件輸入 JSON預期輸出 JSON
根參考指到掛載外{"mount":"/staff/","reference":"/app.js"}{"pathname":"/app.js","insideMount":false}
掛載位置未訂{"mount":null,"reference":"app.js"}{"held":true}
下載可編輯 CSV ↓

原例 app.js 更正後應到哪裏?

查看參考答案

掛載內 /staff/app.js;/app.js 是另一個位於所附子路徑外的目標。

相關資源