Backend fundamentals: building a server
بیک اینڈ کی بنیادیں: سرور بنانا
42 min read
Three ways to see it
A backend is just a long-running program that listens on a port and answers HTTP requests. In Node.js, the simplest example fits in five lines. import { serve } from '@hono/node-server'; import { Hono } from 'hono'; const app = new Hono(); app.get('/health', c => c.json({ ok: true })); serve({ fetch: app.fetch, port: 3001 });. Run that and your laptop is now a web server. Anything more complex (databases, auth, validation) is layers on top of this core. The mental model is: request comes in, function runs, response goes out.
Way one, the runtime. Node.js is JavaScript on the server. Created in 2009, it runs the same V8 engine Chrome uses. In 2026 you also have Bun (faster startup, native TypeScript) and Deno (security-first). For a Pakistani team picking one today: Node.js is safest and has the most documentation, Bun is fastest, Deno is cleanest. Most production projects ship Node.js with TypeScript. Install once: nvm install 20; npm init; npm install hono. You now have a working environment.
Way two, the framework. You can write raw Node.js, but a framework saves you a thousand lines. Express has ruled since 2010. Hono is the modern choice for 2026: faster, edge-friendly, TypeScript-native, works on Vercel, Cloudflare Workers, AWS Lambda, and Node.js with the same code. A Hono route for our FBR NTN check: app.get('/verify/:ntn', async c => { const ntn = c.req.param('ntn'); const result = await callFbrApi(ntn); return c.json(result); });. That is the full route definition. Middleware (logging, auth, rate-limit, CORS) bolts on with one line each.
Quick check
Quick check: what makes modern AI different from a rule-based program?
The why-tree
Why-tree level one: why a server at all when Vercel can serverless? Because some workloads benefit from a long-running process: WebSocket connections, scheduled jobs, large in-memory caches, custom database pooling. For a simple NTN-check endpoint, serverless is fine. For a real-time order tracker on Foodpanda scale, you need a server you control.
Try this with Claude
AI-edge prompt to try with Claude: 'Generate a Hono server in TypeScript that exposes POST /verify for FBR NTN validation. Use zod schemas, a bearer-token auth middleware reading from env, a per-IP rate-limiter, structured logging with pino, and a timeout wrapper around the upstream call. Include error responses for 400, 401, 422, 429, 500, 503 with specific messages. Output a single index.ts under 200 lines.' Read it twice before running.
Sources
Sources and further reading. Node.js documentation (nodejs.org/docs). Hono docs (hono.dev). Express docs (expressjs.com) for the older lineage. zod docs (zod.dev). pino logger (getpino.io). FBR Online Verification developer reference (fbr.gov.pk). OWASP API Security Top 10. The HTTP RFCs 9110 to 9114. The Twelve-Factor App methodology (12factor.net).