Koa§
要运行使用 Koa Web 框架构建的应用,请使用 Unit
使用 unit-dev/unit-devel 包安装 Unit。接下来,安装 Unit 的 unit-http 包
# npm install -g --unsafe-perm unit-http
创建你的应用目录,安装 Koa,并链接 unit-http
$ mkdir -p /path/to/app/ $ cd /path/to/app/ $ npm install koa # npm link unit-http
让我们尝试一下 教程应用 的一个版本,将其另存为 /path/to/app/app.js
const Koa = require('koa'); const app = new Koa(); // logger app.use(async (ctx, next) => { await next(); const rt = ctx.response.get('X-Response-Time'); console.log(`${ctx.method} ${ctx.url} - ${rt}`); }); // x-response-time app.use(async (ctx, next) => { const start = Date.now(); await next(); const ms = Date.now() - start; ctx.set('X-Response-Time', `${ms}ms`); }); // response app.use(async ctx => { ctx.body = 'Hello, Koa on Unit!'; }); app.listen();
该文件应该可执行,以便应用可以在 Unit 上运行
$ chmod +x app.js
运行以下命令,以便 Unit 可以访问应用目录
# chown -R unit:unit /path/to/app/
有关更多详细信息,包括权限,请参阅 安全检查清单。
接下来,准备 Unit 的 Koa 配置
{ "listeners": { "*:80": { "pass": "applications/koa" } }, "applications": { "koa": { "type": "external", "working_directory": "/path/to/app/", "executable": "/usr/bin/env", "arguments": [ "node", "--loader", "unit-http/loader.mjs", "--require", "unit-http/loader", "app.js" ] } } }
上传更新后的配置。假设上述 JSON 已添加到
config.json
# curl -X PUT --data-binary @config.json --unix-socket \ /path/to/control.unit.sock https://#/config/
更新成功后,您的应用应可在侦听器的 IP 地址和端口上使用
$ curl https://# -v * Trying 127.0.0.1:80... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 80 (#0) > GET / HTTP/1.1 > Host: localhost > User-Agent: curl/7.68.0 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Content-Type: text/plain; charset=utf-8 < Content-Length: 11 < X-Response-Time: 0ms < Server: Unit/1.32.1 Hello, Koa on Unit!