Complete Config reference
Set only what should change.
Known nested objects merge with isolated defaults. Values are validated before listeners open.
Using configuration
Pass an object or use Config directly.
const {Server}=require('node-http-server');
const server=new Server({
host:'127.0.0.1',
port:8080,
root:'./public',
server:{
noCache:false,
allowDotfiles:false,
compression:true,
maxRequestBodyBytes:1048576
}
});
server.deploy();| API | Behavior |
|---|---|
new Server(values?) | Creates an instance with an isolated Config. |
server.deploy(values?) | Merges supplied values before deployment. |
new Config(values?) | Creates isolated configuration without deploying. |
config.merge(values) | Safely merges and returns the same Config instance. |
Config.defaults | Returns a fresh copy of all defaults. |
Config.mimeTypes | Returns a fresh built-in MIME map. |
const Config=require('node-http-server/config');
const mimeTypes=require('node-http-server/mime-types');import Config from 'node-http-server/config';
import mimeTypes from 'node-http-server/mime-types';Network, roots, and top-level state
Separate binding from Host routing.
| Key | Default | Purpose | Playground |
|---|---|---|---|
host | '127.0.0.1' | Address passed to listen(). | Location and host routing |
port | 8080 | HTTP port; 0 requests an ephemeral port. | Location and host routing |
root | process.cwd() | Default static root. | Location and host routing |
domain | '0.0.0.0' | Legacy primary Host check; not a bind address. | Location and host routing |
domains | {} | Additional hostname-to-root mappings. | Additional domains JSON |
verbose | false | Sanitized operational console output. | HTTP behavior |
server | table below | Static HTTP behavior and Node timeouts. | HTTP behavior + Timeouts |
https | empty values | Certificate paths and secure listener mode. | HTTPS |
contentType | built-in map | MIME additions/overrides, or false. | File types and restrictions |
restrictedType | {} | Truthy extension keys return 403. | File types and restrictions |
errors | built-in map | Error headers and status bodies. | Logging and errors |
log | false | NDJSON destination or logging off. | Logging and errors |
logBody | false | Include UTF-8 body in request records. | Logging and errors |
logFunction | built in | Custom request-record writer. | Module-only function stub |
host versus domain: host chooses the network interface. domain and domains choose accepted Host values and static roots. A mismatch returns 421.domain:'0.0.0.0' or domain:'*' selects the primary root before domains is consulted. Set a nonwildcard primary domain when using the map.new Server({
root:'./www/default',
domain:'example.test',
domains:{
'docs.example.test':'./www/docs',
'app.example.test':'./www/app'
}
}).deploy();Static server behavior
The server object.
| Key | Default | Purpose |
|---|---|---|
index | 'index.html' | File used for directory requests. There is no directory listing when it is absent. |
noCache | true | Sends no-cache/no-store/must-revalidate directives. false omits them; it does not add a cache lifetime. |
allowDotfiles | false | Only literal true allows dot-prefixed path segments. The default returns 403 before lookup or SPA fallback. |
compression | false | Negotiates Brotli/gzip for accepted compressible static responses; ranges and manual serve() responses are not compressed. |
compressionThreshold | 1024 | Minimum uncompressed bytes before compression is considered. |
spaFallback | false | true uses index; a string chooses another root-relative file. |
timeout | 30000 | Socket inactivity timeout in milliseconds. |
requestTimeout | 300000 | Whole-request timeout in milliseconds. |
headersTimeout | 60000 | Request-header timeout in milliseconds. |
keepAliveTimeout | 5000 | Keep-alive timeout in milliseconds. |
maxRequestBodyBytes | false | Maximum request bytes; false/0 is unlimited. |
404, for an extensionless path whose request accepts HTML or */*. Missing assets and non-HTML requests remain 404.allowDotfiles:true exposes every dot-prefixed segment in the selected root, including /.git, /.env, and /.well-known. Use a dedicated audited root.Limits and timeouts
Every disable value is explicit.
| Setting | false | 0 | Unit |
|---|---|---|---|
server.timeout | Disabled | Disabled | milliseconds |
server.requestTimeout | Disabled | Disabled | milliseconds |
server.headersTimeout | Disabled | Disabled | milliseconds |
server.keepAliveTimeout | Disabled | Disabled | milliseconds |
server.maxRequestBodyBytes | Unlimited | Unlimited | bytes |
contentType | Automatic map removed | Invalid map value | — |
null: false, null, and numeric 0 disable timeouts and the body cap through the module API. The CLI accepts false/off/0, not null.File types and restrictions
Serve, reject, or use the binary fallback.
| Configuration | Result |
|---|---|
| No entry for extension | application/octet-stream. |
contentType:{md:'text/markdown; charset=utf-8'} | Adds or overrides that extension. |
contentType:{secret:false} | Filenames ending in .secret return 415 after path policy checks. |
contentType:false | Removes automatic mapping; served files use application/octet-stream. |
restrictedType:{key:true} | Filenames ending in .key return 403 after path policy checks. |
new Server({
contentType:{
md:'text/markdown; charset=utf-8',
secret:false
},
restrictedType:{
key:true,
pem:true
}
});Extension keys do not include a dot. The built-in map is available through Config.mimeTypes or the node-http-server/mime-types package subpath. If an object is merged after contentType:false, the built-in defaults are restored first and then overlaid.
Error responses
Override headers or individual bodies.
| Key | Default / shape | Use |
|---|---|---|
errors.headers | Content-Type: text/plain; charset=utf-8, X-Content-Type-Options: nosniff | Headers applied to built-in error responses. |
errors[400] | '400 Bad Request' | Malformed request. |
errors[403|404|405] | Status text | Denied, missing, or unsupported method. |
errors[413|415|416|421|500] | Status text | Limit/type/range/Host/internal failures. |
new Server({
errors:{
headers:{'Content-Type':'application/json'},
404:'{"error":"not found"}'
}
});Logging
Append NDJSON or provide a writer.
| Key | Default | Behavior |
|---|---|---|
log | false | File path enables one JSON record per request line. |
logBody | false | Adds the UTF-8 request body when deliberately enabled. |
logFunction | built-in append writer | Receives the record with this bound to Config; may return a Promise. |
The built-in writer adds a timestamp without mutating its input and redacts common credential headers. Serialization/filesystem errors are captured. Protect log destinations and treat any stored request body as sensitive.
new Server({
log:true,
logFunction(data){
return sendRecordSomewhere(data);
}
});HTTPS
Supply key and certificate paths together.
| Key | Default | Purpose |
|---|---|---|
https.ca | '' | Optional CA certificate path. |
https.privateKey | '' | Private-key path. |
https.certificate | '' | Certificate path. |
https.passphrase | false | Optional private-key passphrase. |
https.port | 443 | HTTPS port. |
https.only | false | Skip HTTP when HTTPS is configured. |
const server=new Server({
host:'127.0.0.1',
https:{
privateKey:'/path/to/private.key',
certificate:'/path/to/certificate.pem',
port:8443,
only:true
}
});
server.deploy();Validation and merging
Invalid configuration fails before serving.
| Check | Failure |
|---|---|
| Config is not a plain object | TypeError. |
| Nested map is not a plain object | TypeError naming the key. |
__proto__, constructor, or prototype | Rejected as unsafe. |
| Port not an integer in 0–65535 | Deployment throws. |
| Host empty | Deployment throws. |
| Root/domain root missing or not a directory | Deployment throws. |
| Negative timeout/limit/threshold | Deployment throws. |
| Only one of HTTPS key/certificate set | Deployment throws. |
https.only:true without key/certificate | Deployment throws. |
Merge precedence is defaults, then constructor values, then later deploy()/merge() overlays. Known nested records shallow-overlay and omitted earlier values persist. Instances, Config.defaults, Config.mimeTypes, and user maps do not share mutable nested state. Unknown safe top-level keys are accepted but ignored by the server.