Write handleCreateUser(req, nextId), an HTTP handler for creating a user. It never fails or throws: every outcome is a response { status, body } where body is a plain JSON-serializable object.
req is { method, headers, body }; headers maps header names to values and body is the raw request text. nextId() returns a fresh id; call it only when a user is actually created.
Checks, in order (the first that fails decides the response):
methodmust be exactly"POST", else405 { error: "method_not_allowed" }.- A
content-typeheader must be present (header names are case-insensitive) and its media type, the part before any;, trimmed and compared case-insensitively, must beapplication/json. Parameters such as; charset=utf-8are allowed. Otherwise415 { error: "unsupported_media_type" }. - The body must parse as JSON, else
400 { error: "invalid_json" }. - The parsed body must be a JSON object (not
null, an array, or a primitive), else422 { error: "invalid_field", field: "body" }. - Fields, checked in this order, each failing with
422 { error: "invalid_field", field: "<name>" }:name: a string that is non-empty after trimming.email: a string with no whitespace, exactly one@, at least one character before the@, and a part after the@that contains a.which is neither its first nor its last character.age: optional. Missing ornullmeans not provided. Otherwise it must be an integer number from 0 to 150 inclusive.
On success respond 201 { id, name, email } where id comes from nextId(), name is trimmed, and email is as given. Add age only when it was provided. Other fields in the request body are ignored and never echoed back. Response bodies never contain null or undefined values.