Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Request
✓ import { Request } from 'servie'
✗ const Request = require('servie').Request
Servie primarily uses named exports. For Node.js-only environments in TypeScript, consider `import { Request } from 'servie/dist/node'` to avoid DOM type conflicts.
Response
✓ import { Response } from 'servie'
✗ import Response from 'servie'
Servie's core types are named exports. Ensure you use destructuring for imports. In a pure browser context, you can also use `import { Response } from 'servie/dist/browser'`.
Body
✓ import { Body } from 'servie'
✗ const Body = require('servie')
The `Body` class is the base for `Request` and `Response`. CommonJS `require` is not recommended for Servie in modern applications.
This quickstart demonstrates how to create `Request` and `Response` objects, handle various body types (text, JSON), manage headers, and utilize `AbortController` for request cancellation within a simple Servie-compatible request handler.
import { Request, Response, Body, Headers, AbortController } from "servie";
// A simple Servie-compatible request handler
async function handleRequest(request: Request): Promise<Response> {
const url = new URL(request.url);
console.log(`Received request for: ${url.pathname} with method ${request.method}`);
if (url.pathname === "/greet" && request.method === "POST") {
const name = await request.text();
const responseBody = `Hello, ${name}!`;
const headers = new Headers({ "Content-Type": "text/plain" });
return new Response(responseBody, { status: 200, headers });
}
if (url.pathname === "/json" && request.method === "GET") {
const data = { message: "This is a JSON response", timestamp: new Date().toISOString() };
const headers = new Headers({ "Content-Type": "application/json" });
return new Response(JSON.stringify(data), { status: 200, headers });
}
if (url.pathname === "/abort-test") {
const controller = new AbortController();
const signal = controller.signal;
setTimeout(() => controller.abort(), 100); // Abort after 100ms
try {
await new Promise((resolve, reject) => {
signal.addEventListener('abort', () => reject(new Error('Request aborted')));
// Simulate a long operation that could be aborted
setTimeout(resolve, 500);
});
return new Response("Operation completed before abort.", { status: 200 });
} catch (e: any) {
if (e.message === 'Request aborted') {
return new Response("Operation aborted successfully.", { status: 400 });
}
throw e;
}
}
return new Response("Not Found", { status: 404 });
}
// Example usage:
async function runExamples() {
// 1. Simple GET request
const getRequest = new Request("http://localhost:3000/json", { method: "GET" });
const getResponse = await handleRequest(getRequest);
console.log(`GET /json Status: ${getResponse.status}, Body: ${await getResponse.json().then(data => JSON.stringify(data))}`);
// 2. POST request with a text body
const postRequest = new Request("http://localhost:3000/greet", {
method: "POST",
body: "World",
headers: { "Content-Type": "text/plain" }
});
const postResponse = await handleRequest(postRequest);
console.log(`POST /greet Status: ${postResponse.status}, Body: ${await postResponse.text()}`);
// 3. Aborted request demonstration
const abortRequest = new Request("http://localhost:3000/abort-test", { method: "GET" });
const abortResponse = await handleRequest(abortRequest);
console.log(`GET /abort-test Status: ${abortResponse.status}, Body: ${await abortResponse.text()}`);
}
runExamples().catch(console.error);
Errors
Common errors & fixes
TypeError: response.json is not a function
The `Body` (and thus `Request` or `Response`) can only be consumed once. If you call `text()`, `json()`, or `arrayBuffer()` on a body, subsequent calls will fail.
fixIf you need to read the body multiple times, call `.clone()` first: `const clonedResponse = response.clone(); const data1 = await clonedResponse.json(); const data2 = await response.text();`
ReferenceError: require is not defined
You are attempting to use CommonJS `require()` syntax in an ECMAScript Module (ESM) environment (e.g., `type: "module"` in `package.json` or a `.mjs` file).
fixChange your import statements to use ESM syntax: `import { Request, Response } from 'servie';` Property 'fetch' does not exist on type 'Global' (or similar TypeScript DOM type conflict)
You are likely importing from `servie`'s main entry point in a Node.js TypeScript project, which includes DOM types globally, conflicting with a pure Node.js environment setup.
fixExplicitly import from the Node.js distribution: `import { Request, Response } from 'servie/dist/node';` Alternatively, configure your `tsconfig.json` to include appropriate `lib` entries or set `skipLibCheck: true`. Audit
Dependencies
No dependency data recorded yet.