← 文档目录 · 部署 Node 服务 · 写一个「可被托管」的应用

写一个「可被托管」的应用

好消息:绝大多数应用不用改代码。 反代层会替你处理子路径问题。这一页讲
它到底做了什么、什么情况下还需要你配合。

关键事实:路径前缀在两侧都被处理了

应用挂在 /server/hello-node 下,但它收到的请求路径是去掉前缀的:

用户请求 应用看到的 req.url
/server/hello-node /
/server/hello-node/about /about
/server/hello-node/api/x?y=1 /api/x?y=1

响应侧,LocalHub 会自动做三件事:

  1. 重写 HTML 里的绝对路径

    html
    <link href="/style.css"> → <link href="/server/hello-node/style.css">
    <script src="/app.js"> → <script src="/server/hello-node/app.js">
    <a href="/about"> → <a href="/server/hello-node/about">
    <img srcset="/a.png 1x, /b.png 2x">
    → <img srcset="/server/hello-node/a.png 1x, ...">
    <form action="/submit"> → <form action="/server/hello-node/submit">
    <base href="/"> → <base href="/server/hello-node/">

  2. 重写 CSS 里的资源引用

    css
    .a { background: url(/img/bg.png) } → url(/server/hello-node/img/bg.png)
    @import "/theme.css"; → @import "/server/hello-node/theme.css";

  3. 注入运行时垫片,把 JS 里以 / 开头的请求自动补前缀

    js
    fetch('/api/x') → 实际请求 /server/hello-node/api/x
    new XMLHttpRequest().open('GET','/y') → 同上
    navigator.sendBeacon('/z') → 同上
    new EventSource('/sse') → 同上
    new WebSocket('/ws') → 同上(自动升级为 ws:// / wss://)

不动的东西:https://... 外链、//cdn... 协议相对、data:、#锚点、
相对路径(about、./about)。

但也别依赖它做所有事

重写是基于规则的,不是完整的 URL 解析器。这些情况仍会漏:

情况 例子 建议
路径拼在字符串里传参 fetch(apiBase + '/x') 且 apiBase 来自配置 读 X-Forwarded-Prefix 自己拼
服务端重定向硬编码 res.redirect('/login') → 已自动补前缀 ✓ 无需处理
写进 CSS-in-JS 变量 style={{ background: "url(/a.png)" }} 且是 JS 字符串 用相对路径
<base href> + 相对路径混用 已经自动改成挂载前缀 无需处理
Service Worker 里的 scope register('/sw.js') 用相对路径

推荐写法(最稳)

即使有自动重写,用相对路径仍然是最省心的:

<a href="./about">关于</a>
<link rel="stylesheet" href="./style.css">

前提是页面地址以 / 结尾(/server/hello-node/)。访问不带斜杠的
/server/hello-node 时,相对路径会解析到 /server/ 下 —— 所以关键页面
建议用绝对路径(由反代自动补前缀)或让服务端 302 到带斜杠的地址。

想知道自己被挂在哪:读 X-Forwarded-Prefix

反代会带这些头:

X-Forwarded-Prefix: /server/hello-node
X-Forwarded-Host:   sxd91.kdns.fr
X-Forwarded-Proto:  https
X-Forwarded-For:    <客户端 IP>
X-LocalHub-Hosted-App: hello-node

Express 里:

app.use((req, res, next) => {
  res.locals.base = req.get('x-forwarded-prefix') || '';
  next();
});
app.get('/', (req, res) => res.send(`<a href="${res.locals.base}/about">关于</a>`));

一个完整的最小应用(这样写一定没问题)

const http = require('node:http');

const port = Number(process.env.PORT) || 3000;
const host = process.env.HOST || '127.0.0.1';

http.createServer((req, res) => {
  if (req.url === '/healthz') {
    res.writeHead(200, { 'Content-Type': 'application/json' });
    return res.end('{"ok":true}');
  }
  if (req.url === '/' || req.url === '/index.html') {
    res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
    return res.end(`<!doctype html>
<html><head><meta charset="utf-8"><title>demo</title>
<link rel="stylesheet" href="/style.css">
<style>body{font:16px/1.6 system-ui;max-width:42rem;margin:3rem auto;padding:0 1rem}</style>
</head><body>
  <h1>Hello</h1>
  <p>这个页面没写一行前缀代码,反代会把 /style.css 补成 /server/你的仓库名/style.css。</p>
  <button onclick="fetch('/api/ping').then(r=>r.text()).then(t=>alert(t))">调接口</button>
</body></html>`);
  }
  if (req.url === '/style.css') {
    res.writeHead(200, { 'Content-Type': 'text/css' });
    return res.end('h1{color:#0a0}');
  }
  if (req.url === '/api/ping') {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    return res.end('pong');
  }
  res.writeHead(404); res.end('not found\n');
}).listen(port, host, () => console.log('listening ' + host + ':' + port));

fetch('/api/ping') 会被垫片改写成 /server/<名字>/api/ping;服务端只看到
/api/ping。两边都不用写前缀。

健康检查端点

建议加一个:

if (req.url === '/healthz') { res.writeHead(200); return res.end('ok'); }

然后 workflow 里 health-path: /healthz。

注意:健康检查「通过」的定义是「端口能连上并返回任何 HTTP 响应」。
返回 500 也算通过 —— 它的职责是确认服务起来了,不是确认业务正确。
真正的业务自检请放在 run: 步骤里用 curl 做。

依赖

  • 零依赖应用最简单:仓库里直接有 .js,不需要 npm install
  • 要装依赖就在 workflow 里先 run: npm ci --omit=dev
  • 原生模块(node-gyp)需要目标机有编译工具链

关于数据库

SQLite(node:sqlite)可以,但文件要放在稳定目录(见
运行时)。注意:部署时会做代码快照,
工作目录会变 —— 数据库文件绝不能放工作目录里。