Skip to content
中文

MockGPT Node: a minimal model service and HTTP mounting ​

Use the same MockGPT configuration as a standalone model API or mount it into an existing Node HTTP service.

@jangts/agent-core/mock/node is a Node-only entry. createMockServer({mock,maxBodyBytes?,requestTimeoutMs?,allowedOrigins?,allowedHosts?,closeTimeoutMs?}) returns listen({host?,port?}) and close. createMockHandler uses the same request policy and returns a Node IncomingMessage/ServerResponse handler, with the host owning listening and shutdown.

By default it only accepts loopback, local addresses reached through this connection, and explicitly bound hostnames. Host is checked before Origin, allowedHosts holds exact hostnames/IPs and does not accept wildcards; allowedOrigins separately allows cross-origin and cannot let an invalid Host through. Request body, timeout, and shutdown wait are bounded. Calling listen repeatedly during startup, listening, or shutdown is rejected with MOCK_ACTIVE; close waits for startup and for the same shutdown, and listening is allowed again afterwards.

The HTTP shell converts generation errors into error responses that carry a code; responses.reject closes the connection, unlike an explicit HTTP error. It fails when nothing matches and there is no fallback, and it never connects to a real model. The mock service provides no business authentication; the host configures the access boundary when exposing it.

Progressive examples for the same business case ​

Start a minimal model API and close it when done ​

Beginner · Node.js

ts
import { MockGPT } from '@jangts/agent-core/mock';
import { createMockServer } from '@jangts/agent-core/mock/node';
const mock = new MockGPT({ rules: [{ prompt: '你好', generator: '你好。' }] });
const server = createMockServer({ mock });
try {
  const address = await server.listen({ host: '127.0.0.1', port: 0 });
  console.log(address);
  // 将输出地址的 /v1 作为模型客户端 base URL。
} finally { await server.close(); }
js
import { MockGPT } from '@jangts/agent-core/mock';
import { createMockServer } from '@jangts/agent-core/mock/node';
const mock = new MockGPT({ rules: [{ prompt: '你好', generator: '你好。' }] });
const server = createMockServer({ mock });
try {
    const address = await server.listen({ host: '127.0.0.1', port: 0 });
    console.log(address);
    // 将输出地址的 /v1 作为模型客户端 base URL。
}
finally {
    await server.close();
}

Mount into an existing Node HTTP lifecycle ​

Configuration · Node.js

ts
import { createServer } from 'node:http';
import { MockGPT } from '@jangts/agent-core/mock';
import { createMockHandler } from '@jangts/agent-core/mock/node';
const mock = new MockGPT({ rules: [{ generator: '演示回复' }] });
const handler = createMockHandler({ mock, requestTimeoutMs: 5000 });
const server = createServer(handler);
server.listen(4317, '127.0.0.1');
// 宿主退出时关闭 server;需要自定义域名时显式配置 allowedHosts。
js
import { createServer } from 'node:http';
import { MockGPT } from '@jangts/agent-core/mock';
import { createMockHandler } from '@jangts/agent-core/mock/node';
const mock = new MockGPT({ rules: [{ generator: '演示回复' }] });
const handler = createMockHandler({ mock, requestTimeoutMs: 5000 });
const server = createServer(handler);
server.listen(4317, '127.0.0.1');
// 宿主退出时关闭 server;需要自定义域名时显式配置 allowedHosts。

API reference, usage examples and developer guides.