MIMEType Parses Media Type Parameters
Parse media type strings with the MIMEType class: type, subtype, essence, and a MIMEParams map you can read, iterate, mutate, and re-serialize.
Code
import { MIMEType } from 'node:util';
// MIMEType parses a media type string into type, subtype, and parameters
const mime = new MIMEType('text/html; charset=utf-8; boundary=gc0p4Jq0M');
console.log('type:', mime.type);
console.log('subtype:', mime.subtype);
console.log('essence:', mime.essence);
console.log('charset:', mime.params.get('charset'));
// params is a MIMEParams map: iterate, test, and mutate it
for (const [name, value] of mime.params) {
console.log('param:', name, '=', value);
}
console.log('has boundary:', mime.params.has('boundary'));
mime.params.set('charset', 'utf-16');
mime.params.delete('boundary');
console.log('re-serialized:', mime.toString());Output
Captured from a sandboxed run of this snippet in the build pipeline.
type: text
subtype: html
essence: text/html
charset: utf-8
param: charset = utf-8
param: boundary = gc0p4Jq0M
has boundary: true
re-serialized: text/html;charset=utf-16How it works
The cycling part of this snippet is for (const [name, value] of mime.params). node:util supplies MIMEType — built-in surface, no package involved.
Top-level bindings mime hold the demo state — nothing sits inside a function scope.
Caveats
node:utilis a Node.js built-in module — it ships with the runtime, so there is nothing to install.
Using it
The run emits 7 labeled sections — type:, subtype:, essence:, charset:, param:, has boundary:, re-serialized: — one per step.
Key statements
const mime = new MIMEType('text/html; charset=utf-8; boundary=gc0p4Jq0M');
mime.params.set('charset', 'utf-16');
mime.params.delete('boundary');
Interactive Tools for This Topic
FAQ
- What does this import from outside the project?
- Only `node:util`, and that is a Node.js built-in — it comes with the runtime.
Report a problem with this exampleOpens a pre-filled issue on GitHub.