Posts

Showing posts with the label JavaScript

Koa Tutorial: Build a Node.js Orders API From Scratch

Image
Quick answer Koa is a small Node.js web framework built by the team behind Express: it gives you a server, a context object, and async middleware, and leaves routing and body parsing to add-on packages. This post builds a small orders API with three routes, lists two seeded orders, creates a third, and fixes a real 400 error when the body never arrives. A small shop in Karachi takes orders over the phone and writes each one in a paper notebook. When a customer calls to ask where an order is, someone flips pages. A simple web API would fix that: list orders, add one, fetch one by number. Express is the usual answer in Node.js, but Koa takes the same idea and makes it smaller: no built-in router, no built-in body parser, just a context object and middleware that reads like plain async code. The rest of this post builds that API, and breaks it once, so you see what a missing piece looks like. What I would check before using it My first check is whether the team is comfortable with asy...

Turborepo Tutorial: Run Every Package With One Command

Image
Quick answer Turborepo is a build tool for JavaScript and TypeScript monorepos: one command builds every package in the right order and caches the results, so an unchanged package is never rebuilt. This post builds a small shop monorepo with a shared pricing package and a shop app, then shows the second build finishing in 17 milliseconds because the cache hits. A small online shop in Karachi sells kurtas and dupattas. The shop app prices each order, the admin page prices refunds, and a checkout helper prices the cart, and each of the three copies the same discount rule: 10 percent off orders of Rs 10,000 or more, plus 5 percent tax. One day the discount changes to 15 percent, two copies get updated, and the third keeps charging the old price until a customer complains. A monorepo fixes the copying by putting the shared code in one package that every app imports. Turborepo makes that monorepo practical: it runs each package's build in dependency order and remembers the results. W...

Socket.IO Tutorial: Real-Time Order Updates in Node.js

Image
Quick answer Socket.IO is a library that lets a Node.js server push events to connected browsers the moment something happens, instead of waiting for the browser to ask. This post builds a live order-status feed: order 1042 moves from received to packed to delivered, and each change reaches the customer instantly. It fits order tracking, chat, and live dashboards. A customer buys a kurta and a dupatta, pays Rs 12,400, and then waits. The shop page says "processing" and the customer refreshes, waits, refreshes again. The usual fix is polling: the browser asks the server every few seconds whether anything changed. Most of those requests return the same answer, and the one that matters can still arrive seconds late. A push model flips the pattern: the server speaks first, the moment the order changes. Socket.IO is the most used way to do that in Node.js, and this post puts it to work on a real order, with one event that goes missing. What I would check before using it My fir...

pnpm: One Copy of Every Package, Shared Across Projects

Image
Picture a laptop that has hosted a dozen JavaScript projects. The same version of Express sits in a dozen node_modules folders. It works, and on a fast connection nobody notices, until the disk fills up or a CI runner installs everything from scratch on every push. The waste is not dramatic, it is steady: a few minutes here, a few gigabytes there, and a node_modules folder that becomes the biggest thing on the machine. pnpm goes after that waste. It keeps one copy of each package version in a shared store and links it into every project that asks for it. Is that saving worth changing how installs work? Quick answer Yes, pnpm is worth it when several JavaScript projects share the same packages. It keeps one copy of each package version in a shared store and links it into every project, so installs are faster and use less disk space. Its strict node_modules layout also exposes packages the code uses but never declared, which keeps dependency lists honest. What I would check before u...

esbuild: Bundle JavaScript in Milliseconds, Not Seconds

Image
You change one line in a small project, save, and wait. The bundler thinks, the terminal scrolls, and a few seconds pass before the page refreshes. Do that forty times in an afternoon and you have lost several minutes to staring at a spinner, plus the focus you lose every time the flow breaks. Most bundlers are slow because they are written in JavaScript and parse everything with a JavaScript parser. For the language underneath the bundler, see JavaScript Explained: The Language That Runs the Whole Web . esbuild is written in Go, parses in parallel, and treats bundling as a compile step rather than a scripting task. The question is whether that speed is worth changing tools for, and what you give up in return. Quick answer Yes, esbuild is worth switching to when the build is slow enough to notice, because it bundles the shop calculator in 4 milliseconds instead of seconds. It is written in Go and parses in parallel, so two files become one 1.5kb bundle that prints the same receipt as...

React Router: Pages That Change Without Reloading

Image
Open a shop site and click a product. The address bar changes, the page changes, and nothing reloads. Visitors expect that smooth switch now, and it is what makes a single-page app feel finished rather than half built. Without a router, the usual workaround is a pile of state: a variable holding the current view, a switch statement rendering one component or another, and links that are really buttons wearing a disguise. It works for two pages, then starts to creak. The address bar never changes, so a visitor cannot bookmark a product, share its link, or use the back button properly. React Router closes exactly that gap, and the question is when it is worth adding. Quick answer Yes, React Router is worth adding when an app has three or more distinct views that people share links to. It maps each URL to a component, so /products shows the list and /products/2 shows the ceramic mug, and the address bar, back button, and bookmarks all keep working because Link renders as an ordinary anch...

Fastify: A Fast Node API Without the Ceremony

Picture a small shop that takes orders through a web page. A customer fills in a form, the browser sends the order as JSON, and a Node server has to check the fields, save the order, and answer with the new order id. For the language underneath Node, see JavaScript Explained: The Language That Runs the Whole Web . The work itself is small. The ceremony around it is not: parsing the body, checking that every required field is present, sending back the right status code when something is missing, and keeping the routes readable as they grow. Express leaves most of that to you, which is fine until the third route starts looking like the first two. That gap is where Fastify earns a look. Quick answer Yes, Fastify is worth using for a JSON API with several endpoints when you want validation and structure built in, because a JSON schema rejects bad requests before your handler runs and the client gets a clear 400 instead of a crash later. Handlers stay short by returning values instead of ...

Zustand: React State Without the Boilerplate

Picture a small shop page with a cart icon in the corner. A visitor adds a tote bag and a mug. Three parts of the page need to know what happened: the cart icon showing the count, the cart drawer listing the items, and the checkout button showing the total. If each part keeps its own copy of the cart, the numbers drift apart. The usual fix is to lift the state up to a shared parent, but that turns every middle component into a courier for data it never uses. That is the problem Zustand sets out to solve, and the question is whether a tiny store does the job better than the tools React already ships with. Quick answer Yes, Zustand is worth using when several distant components share state that changes a lot, because a tiny store holds the cart once and every part reads from it. Each component picks just the slice it needs, so the cart icon re-renders on a count change and ignores a price edit, and the total is computed from the items so it can never drift out of sync. Skip it when the...