aardio simpleHttpServer.startSpaUrl参数详解

startSpaUrl 的三个参数

根据文档,函数签名是:

aardio 复制代码
wsock.tcp.simpleHttpServer.startSpaUrl(indexHtmlPath, documentBase, app)
参数 必填 说明
indexHtmlPath ✅ 是 SPA 单页应用首页路径,如 "/res/index.html"。404 错误页也会自动设为这个路径
documentBase ❌ 可选 指定网站根目录,解决网页里用绝对路径(如 /css/app.css)找不到的问题。支持硬盘目录和资源目录,如 "/res/"
app ❌ 可选 自定义 HTTP 请求处理函数,签名:function(response, request, session)

三个参数的完整用法

aardio 复制代码
import wsock.tcp.simpleHttpServer; // ⚠️ 必须在 import web.view 前面
import web.view;

var wb = web.view(winform);

var url = wsock.tcp.simpleHttpServer.startSpaUrl(
    // 参数1:首页路径(必填)
    "/res/index.html",

    // 参数2:documentBase(可选)- 让你网页里用 / 开头的绝对路径能正确映射到 /res/ 下
    ,  // 省略,不设额外根目录

    // 参数3:自定义 handler(可选)
    function(response, request, session) {
        if (string.startWith(request.path, "/videos/")) {
            // 视频走硬盘
            var filePath = io.fullpath("~/videos/" + string.slice(request.path, 9));
            response.contentType = /* 设 MIME */;
            response.loadcode(filePath);
        } else {
            // 其他走默认 /res/ 内嵌资源
            response.loadcode(request.path);
        }
    }
);

关键点

  • startSpaUrlstartUrl 多了一个 SPA 路由回退能力------所有不存在的路径都返回 index.html,让 Vue Router 处理

  • 参数2 documentBaserequest.path / request.pathInfo 的关系:

    如果 documentBase = "/res/"
    请求 /res/css/app.css → request.path = "/res/css/app.css"
    → request.pathInfo = "/css/app.css" ← 去掉了 documentBase 前缀

  • 参数2 大多数时候不传就行 ,除非你网页里硬编码了 / 开头的绝对路径

注意:mainThread() 返回的 HttpSimpleServerMainObject 也有 startSpaUrl(indexHtmlPath, documentBase),但那个版本没有第三个参数 app 。要用自定义 handler 的话,直接用 wsock.tcp.simpleHttpServer.startSpaUrl(...) 这个静态方法。

相关推荐
toooooop85 天前
aardio WebView 同源策略与跨域终极解决方案
webview·aardio
恶猫1 个月前
自动拨号换ip软件简单实现。aardio版。
java·网络·aardio·adsl·换ip·rasphone.exe·rasdial.exe
白叔King2 个月前
aardio时间日期转换中文时间
前端·javascript·aardio
dalong103 个月前
A27:图像九宫格分割程序
笔记·aardio
dalong103 个月前
A26:扫雷游戏
笔记·游戏·aardio
dalong103 个月前
A25:捕获鼠标与按键事件
笔记·aardio
dalong103 个月前
A24:圈住小猫游戏
笔记·算法·游戏·aardio
dalong104 个月前
A14:自定义动画演示
笔记·aardio
dalong104 个月前
A11:plus 控件窗口绘图基础
笔记·aardio