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);
}
}
);
关键点
-
startSpaUrl比startUrl多了一个 SPA 路由回退能力------所有不存在的路径都返回index.html,让 Vue Router 处理 -
参数2
documentBase和request.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(...)这个静态方法。