httpListener
Official sources are open to Staxis GitLab group members: how to join and view the code.
Repository:
httpListener development
Versions:
tags
and releases
Description
httpListener starts the HTTP server from the application already prepared by serverApp. Its label and typology are both httpListener. It binds the selected host and port, applies server timeouts, tracks connections for shutdown, and publishes the real server object for authorized consumers. Middleware and routes belong to the application prepared before it.
Read before flight
-
We suggest choosing the bind address deliberately. The supplied virtualized=false setting listens on 0.0.0.0; use virtualized=true with hostname='127.0.0.1' for local-only development. hostname here selects a network bind address, not an HTTP virtual-host policy.
-
We suggest tuning timeouts for your uploads, clients, and proxy setup. Request-receiving timeouts are not execution deadlines for application jobs. Keep resource limits in the appropriate request handlers as well.
-
We suggest keeping one owner for listener startup and shutdown, checking actual listening readiness before announcing service availability, and testing shutdown with active requests or upgraded connections. Coordinate any consumers that use the published server methods.
Requirements and Installation
The required pack is serverApp, whose supplied manifest already permits httpListener's label. Native Node.js listening needs no additional npm package. Install through the Manager:
manager -> install -> httpListener
Prepare the HTTP application before this pack: serverApp, request-policy middleware, your routes, static or download handlers, and the selected fallback handling. Place consumers that require the live listener after httpListener. Prepare project keys and integrity records. Let the pack attach automatically; avoid an additional serverApp.listen call for the same application.
Files Map
| File | Purpose |
|---|---|
| manifest.json | Declares the serverApp requirement and authorized listener consumers |
| configs.js | Provides port, host selection, and timeout defaults |
| main.js | Starts the listener, publishes the server, tracks sockets, and initiates shutdown |
| README.md | Pack reference and consumer example |
Configuration
In the project's config.js, settings belong to config.serveropt.listener. Merge the following serveropt branch into the exported config object, preserving the application's other server settings. This example binds locally; the supplied defaults instead use virtualized=false and hostname='localhost'. The timeout and port values below are the supplied defaults.
// config.js -> config (merge into the exported object)
serveropt: {
listener: {
port: 3000,
mainTimeout: 7000,
headersTimeout: 1000,
requestTimeout: 5000,
keepAliveTimeout: 1000,
virtualized: true,
hostname: '127.0.0.1'
}
}
| Option under config.serveropt.listener | Behaviour |
|---|---|
| port | Configured port takes precedence over process.env.PORT; fallback is 3000 |
| virtualized | false selects 0.0.0.0; true selects hostname |
| hostname | Bind host when virtualized is true |
| mainTimeout | Milliseconds assigned to server.timeout for socket inactivity |
| headersTimeout | Milliseconds allowed to receive complete request headers |
| requestTimeout | Milliseconds allowed to receive the complete incoming request |
| keepAliveTimeout | Keep-alive inactivity setting after a response |
Port and timeout settings use fallback values when zero or otherwise falsy. Port 0 therefore does not request an automatically allocated port here. With the supplied port=3000, setting PORT in the environment alone does not override it. If headersTimeout exceeds requestTimeout, the pack logs a warning and changes headersTimeout to half of requestTimeout.
The implementation's omitted-value timeout fallbacks are 10000, 5000, 10000, and 5000 milliseconds respectively; the installed configs.js supplies the shorter values shown above. These fields are applied to the server object; their details depend on the Node.js runtime. See the Node.js HTTP server reference.
Startup and a Local Check
The pack obtains serverApp and starts the selected adapter. Native, Express, and Koa use listen(port, host), which returns the server. Fastify uses await listen({ port, host }) and then reads the adapter's server property. The native path starts a plain HTTP server; this pack does not load TLS certificates or configure HTTPS by itself.
node project start
// From another terminal, after startup
curl -i http://127.0.0.1:3000/
Adjust the URL to your configuration. The response body and status come from the middleware and routes you prepared, not from httpListener. For example, an otherwise unresolved request can receive an HTML fallback from httpKindness. Native listen is asynchronous: distinguish obtaining the server object from reaching its listening event.
Request the Live Server
The attach bundle contains host and port metadata, while its assets contain the actual listener. The supplied permits list includes websocket and healthz. To use the generic consumer below, add someOtherLayer's manifest.label to httpListener's permits, declare its dependency, and place it after httpListener in the stack. Replace that example label with your own pack label.
const token = engine.vault.requireAccess(
'someOtherLayer', // Requester's manifest.label
'httpListener' // Provider's manifest.label
)
if (typeof token !== 'string' || token.startsWith('error:')) {
throw new Error('httpListener access denied')
}
const bundle = layers.require(
'httpListener', // Provider's manifest.typology: runtime slot
token,
false
)
const listener = bundle?.assets
if (!listener || typeof listener.on !== 'function') {
throw new Error('httpListener is unavailable')
}
const boundAddress = listener.address()
address() reports the actual binding when available; it can be null before listening or after closure. Normal Node.js server events and methods are available on the published object. Manifest permits control access to that object, not individual calls such as close or event registration. Assign those responsibilities deliberately among your consumers.
Shutdown Behaviour
Detach initiates server.close to stop accepting connections. It marks the service as stopping, reduces a longer keepAliveTimeout to 1000 milliseconds, and disables TCP keep-alive on tracked sockets. A hardcoded 30-second timer destroys remaining tracked sockets if closure is still pending. Upgrade attempts observed while stopping have their socket destroyed.
The detach bundle's assets contain the shutdown Promise rather than the ordinary server object. That Promise resolves with closedAt when the close callback runs; the no-listener branch can additionally report skipped=true. Obtaining the detach bundle alone does not establish that every connection has closed. Ensure your shutdown coordinator waits for the relevant completion result.
We suggest using a controlled application restart for configuration changes and verifying shutdown with your real request and upgrade consumers. The 30-second shutdown deadline is separate from the request-receiving timeouts configured above.
Documentation
This page is the HTML adaptation of the pack's README.md, with practical clarifications checked against the implementation. Complete Staxis onboarding to request GitLab access to the official sources.