मुख्य सामग्री पर जाएँ
Skillpedia
API और सर्वर (backend)7.8k · #13088

vanilla-ts-http-route-handler

यह क्या करती है

यह स्किल बताती है कि उस Node सर्वर में API रूट कैसे जोड़ें जो बिल्ट-इन http मॉड्यूल और switch-आधारित राउटर इस्तेमाल करता है। यह छोटे TypeScript सर्वर पर काम करने वाले डेवलपर्स के लिए है।

कब इस्तेमाल करें

  • ✓जब आपका सर्वर बिल्ट-इन http मॉड्यूल इस्तेमाल करता है और आपको नया रूट चाहिए।
  • ✓जब आप छोटे सर्वर में Express या दूसरा फ़्रेमवर्क जोड़ने से बचना चाहते हैं।
  • ✓जब दूसरी रूट फ़ाइलें ऐसी फ़्रेमवर्क शैली में हैं जो आपके सर्वर से मेल नहीं खातीं।

Skill का लिंक

https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/vanilla-ts-http-route-handler/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/vanilla-ts-http-route-handler/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“क्या आप मेरे Node सर्वर में बिना Express इंस्टॉल किए एक नया हेल्थ चेक रूट जोड़ सकते हैं?”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

एजेंट एक रूट फ़ाइल बनाता है जो बिल्ट-इन request और response टाइप इस्तेमाल करती है, फिर उस रूट को सर्वर के switch स्टेटमेंट में रजिस्टर करता है।

तकनीकी सामग्री (मूल SKILL.md)

लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।

Adding Route Handlers to a Vanilla TypeScript HTTP Server

Use this skill whenever you need to add a new route to a TypeScript project that uses Node's built-in http module and a switch/case pathname router without Express or Next.js.


Step 0 — Confirm the routing style

Before writing any code, inspect server/index.ts (or whichever file starts the HTTP server). Look for:

http.createServer((req, res) => {
  const { pathname } = new URL(req.url!, `http://${req.headers.host}`);
  switch (pathname) {
    case '/api/foo': ...
  }
});

If you see this pattern, follow the steps below.
Do NOT use express.Router, next/server, or any framework-specific handler signature — even if other files in server/routes/ happen to use those patterns.


Step 1 — Create the route file

Create server/routes/<feature>.ts.
Use only the native Node types: http.IncomingMessage and http.ServerResponse.

// server/routes/health.ts
import http from 'http';

export async function handleHealth(
  req: http.IncomingMessage,
  res: http.ServerResponse
): Promise<void> {
  // Parse body for POST/PUT if needed
  const body = await readBody(req);   // helper shown in Step 3

  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ status: 'ok' }));
}

Rules for the route file:

  • Export the handler as a named async function (handle<Feature>).
  • Accept exactly (req: http.IncomingMessage, res: http.ServerResponse).
  • Return Promise<void>.
  • Do not call next() or return a Response object.
  • Handle errors internally and write an appropriate HTTP status + JSON body.

Step 2 — Import the handler in server/index.ts

Add a named import at the top of the file alongside any existing imports:

import { handleHealth } from './routes/health';

Step 3 — Add a case entry to the switch block

Locate the existing switch (pathname) block and add a new case:

switch (pathname) {
  case '/api/health':
    await handleHealth(req, res);
    break;

  // … existing cases …

  default:
    res.writeHead(404, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ error: 'Not found' }));
}
  • Match the exact pathname string used by the client.
  • Always break (or return) after calling the handler.
  • Keep the default case last.

Step 4 — Optional: shared body-reading helper

If multiple routes need to parse a JSON request body, add a small utility rather than duplicating the logic:

// server/utils/readBody.ts
import http from 'http';

export function readBody(req: http.IncomingMessage): Promise<unknown> {
  return new Promise((resolve, reject) => {
    let data = '';
    req.on('data', (chunk) => (data += chunk));
    req.on('end', () => {
      try {
        resolve(data ? JSON.parse(data) : {});
      } catch {
        reject(new Error('Invalid JSON'));
      }
    });
    req.on('error', reject);
  });
}

Import and use it inside any route handler that needs it.


Checklist

  • server/routes/<feature>.ts exists and exports a named async handler.
  • Handler signature is (req: http.IncomingMessage, res: http.ServerResponse): Promise<void>.
  • Handler always calls res.writeHead(...) and res.end(...) on every code path.
  • Handler is imported in server/index.ts.
  • A matching case entry is present in the switch (pathname) block.
  • No Express / Next.js / Koa types or patterns were introduced.

Anti-patterns to avoid

❌ Wrong ✅ Correct
import { Router } from 'express' import http from 'http'
export default function handler(req: NextApiRequest, ...) export async function handleFoo(req: http.IncomingMessage, ...)
router.get('/foo', ...) case '/api/foo': await handleFoo(req, res); break;
Returning a value from the handler Calling res.end(...) and returning void
अक्सर पूछे जाने वाले सवाल

Express क्यों नहीं?

स्किल उन सर्वरों के लिए है जो पहले से सादे Node इस्तेमाल करते हैं। Express जोड़ने से पूरे सर्वर के काम करने का तरीका बदल जाता।

मुझे कैसे पता चलेगा कि मेरा सर्वर कौन सी शैली इस्तेमाल करता है?

स्किल कहती है कि मुख्य सर्वर फ़ाइल में अनुरोध पाथ पर switch देखें।

क्या रूट अनुरोध की बॉडी पढ़ सकता है?

हां। स्किल में POST और PUT अनुरोधों के लिए बॉडी पढ़ने वाला हेल्पर शामिल है।

जानकारी

लेखक का मूल विवरण

How to correctly add route handlers in a vanilla TypeScript project that uses http.createServer with a manual switch/case router — without introducing Express Router or Next.js patterns.

repo: HKUDS/AgentPilot

लोकप्रियता

GitHub स्टार
7.8k
रैंकिंग में स्थान
#13088
लाइसेंस
MIT
अपडेट की गई
1/11/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

मिलती-जुलती skills