type Result<T, E = string> = { ok: true; value: T } | { ok: false; error: E };↵
↵
function parsePort(input: string): Result<number> {↵
const n = Number(input);↵
if (!Number.isInteger(n)) return { ok: false, error: "not a whole number" };↵
if (n < 1 || n > 65535) return { ok: false, error: "out of range" };↵
return { ok: true, value: n };↵
}↵
↵
const port = parsePort(process.env.PORT ?? "8080");↵
if (port.ok) {↵
console.log(`listening on ${port.value}`);↵
} else {↵
console.error(`bad PORT: ${port.error}`);↵
}
Click here or press any key to type