Skip to content

Parse Query String Parameters

Read query parameters from an incoming request with WHATWG URL and searchParams, using a fallback when the key is missing.

Language: Node.js

Added on: 21st August 2026

import http from "node:http";
import { URL } from "node:url";

http.createServer((req, res) => {
  const url = new URL(req.url, "http://localhost");
  const name = url.searchParams.get("name") ?? "friend";
  res.end(`Hi ${name}`);
}).listen(3000);

Caveats

ES module: run as .mjs, or set type module in package.json. Listens on port 3000. The http://localhost base is only for parsing; it is not the listen address. searchParams.get returns the first value only.