@antelopejs/api
羚羊JS的HTTP和WebSocket服务器模块. 它执行节点上的 API 接口 http, (中文(简体) ), https,以及 ws 服务器,因此控制器和其他模块以 @antelopejs/interface-api 回答请求.
部分 羚羊JS用于 羚羊JS 隐形物,用于您的 PostgreSQL 、 MongoDB 或 RethinkDB 数据库的开源后台.
接口
| 接口 | 文档 |
|---|---|
@antelopejs/interface-api | API 密码 |
@antelopejs/interface-api-util | API UTI 软件 |
界面页面文档控制器,路由,参数注入,前缀和后缀处理器,以及WebSocket处理器.
安装
ajs project modules add @antelopejs/api
命令写入模块的默认配置, 该配置是一个 HTTP 服务器 localhost:8080。在一个模块导入的项目中 @antelopejs/interface-api, (中文(简体) ), ajs project modules install 当还没有执行接口时,也添加了此模块.
配置
import { defineConfig } from "@antelopejs/interface-core/config";
export default defineConfig({
name: "my-project",
modules: {
api: {
source: {
type: "package",
package: "@antelopejs/api",
version: "^1.3.4",
},
config: {
servers: [
{ protocol: "http", host: "localhost", port: 8080 },
{ protocol: "https", port: 8443, cert: "<pem>", key: "<pem>" },
],
publicBaseUrl: "https://api.example.com",
strictPort: false,
autoListen: true,
cors: {
allowedOrigins: ["https://app.example.com", /\.example\.com$/],
allowedMethods: ["GET", "POST", "PUT", "DELETE"],
allowedHeaders: ["content-type", "authorization"],
credentials: true,
maxAge: 600,
},
},
},
},
});
| 密钥 | 默认 | 说明 |
|---|---|---|
servers | 端口上的一个 HTTP 服务器 80 | 服务器要启动 。 每个条目都使用 protocol (单位:千美元)http 或者说 https),可选 host 财务报告和审定财务报表 port,以及任何节点.js http.ServerOptions 或者说 https.ServerOptions。 。 。 . |
publicBaseUrl | API_LOCAL_BASE_URL 单位: dev | 浏览器用来到达API的出处. 需要外出开发,没有它,靴子就失效了. |
strictPort | false | 在开发中,失败而不是倒回下一个自由港. 外出开发,模块总是将准确的端口绑起. |
autoListen | true | 设定为 false 创建服务器,并仅在模块调用时为服务器服务 Listen()。 。 。 . |
cors | 无 | 中央统计局政策. allowedMethods 默认为 GET,HEAD,PUT,PATCH,POST,DELETE,以及 credentials 默认为 true。在运行时修改它 SetCorsConfig()。 。 。 . |
在开发中,回回回回回回源(Clocalhost, (中文(简体) ), 127.0.0.1, (中文(简体) ), [::1],任何端口)通过 CORS 不配置。 列出所有其它来源 allowedOrigins。 。 。 .
配置变量
该单元出版三部 配置变量每个都来自第一个条目: servers。其他模块用 ${@api.<NAME>}时 api 是模块的密钥在 modules。 。 。 .
| 变量 | 数值 |
|---|---|
API_PORT | 第一个服务器绑定的端口 . |
API_LOCAL_BASE_URL | 例如,同一宿主的起源 http://127.0.0.1:8080。 。 。 . |
API_PUBLIC_BASE_URL | publicBaseUrl,没有跟踪鞭打. |
modules: {
"file-storage-local": {
config: { baseUrl: "${@api.API_PUBLIC_BASE_URL}" },
},
},
模块在任何其他模块构建之前绑定其收听套接字,所以 API_PORT 总是服务器服务的端口.
软件包命名了计算它们的函数, configVars 内 dist/config-vars.js输入 antelopeJs.configVarsFile。读取项目而不运行它的工具,例如 ajs dms build,解决 ${@api.<NAME>} 与该功能。 这样的话 API_PORT 是配置的端口,并且 API_PUBLIC_BASE_URL 倒计时 API_LOCAL_BASE_URL 何时 publicBaseUrl 未设置.
启动门
该模块为端口服务 construct 继续 在核心宣布每个模块都开始之前,它用 503,躯体 error.server_starting,以及 Retry-After: 1。 。 。 . @antelopejs/core 发送通知为 Events.ModulesStarted 联 合 国 @antelopejs/interface-core/modules当整个项目在停工时停止(Events.ModulesStopping),模块答案 503 再来一次 停止,重新装入,或者在运行时移除单个模块会让出出门.
发射时未能启动的模块离开项目答案 503。同样 start() 120秒后仍未解决 导致启动失败 门所持请求没有运行,所以客户端可以重试. 模块在门关上时回答 CORS 预飞, 因此浏览器可以读取 503。 。 。 .
GET /_live 总是回答 200用它做活性探测器 用正常路线做准备探测器.
快开始
在服务路线的模块中,添加接口.
pnpm add @antelopejs/interface-api
在模块条目中宣布一个控制器 。 它的装饰器在模块装入时会登记路径.
import { Controller, Get, Parameter } from "@antelopejs/interface-api";
export class HelloController extends Controller("/greetings") {
@Get()
hello(@Parameter("name", "query") name = "world") {
return { message: `Hello ${name}` };
}
}
export function construct(): void {}
export function destroy(): void {}
export function start(): void {}
export function stop(): void {}
@Get() 没有位置使用方法名称,所以路由是 /greetings/hello。运行项目时使用 ajs project dev,然后调用 GET http://localhost:8080/greetings/hello?name=Ada。 。 。 .
文档
- API 密码 文档
@antelopejs/interface-api与本模块执行的合同. - API UTI 软件 文档
@antelopejs/interface-api-util与本模块执行的合同. - 配置 文档模块来源,
config区块和配置变量antelope.config.ts。 。 。 .
兼容性
| 软件包 | 支助 | 测试过 |
|---|---|---|
@antelopejs/core | >=1.17.0 <2 | 1.20.0 |
@antelopejs/interface-api | >=0.2.0 <0.4.0 | 0.2.0 |
@antelopejs/interface-api-util | >=0.3.0 <1.0.0 | 0.3.0 |
@antelopejs/interface-core | >=0.4.0 <1.0.0 | 0.4.0 |
生成的表格列表 @antelopejs/core,该模块要求作为对等依赖的模块,以及它所加载的接口包,其范围从 package.json。如果另一个已装入的模块宣布一个排除本模块载重复制范围, @antelopejs/core 拒绝启动项目。 报告 Incompatible interface package resolution 并命名模块、其范围及其找到的副本.
捐款
pnpm install
pnpm dev # run the playground/ project in watch mode
pnpm lint
pnpm format:check
pnpm knip
pnpm test # build, then ajs module test
读取 捐款准则 在打开拉出请求之前.