Extension boundaries
Pick the smallest extension
A simple lookup belongs in map; a small custom calculation may fit njs; a long-running business workflow belongs in an application service. OpenResty combines Nginx-derived infrastructure with LuaJIT and additional modules; it is not njs and its Lua APIs require a separate compatibility review. Native C modules may offer integration with low-level processing but have a larger memory-safety and binary-compatibility review burden. Do not introduce a new runtime only to replace three clear directives.
Fixed lookup → mapSmall pure transformation → njs (if needed)Complex workflow → application serviceLua ecosystem → separately reviewed OpenResty deploymentSeparate HTTP and stream callbacks
HTTP handlers have a request object and may operate on HTTP metadata. Stream handlers operate on TCP/UDP sessions, not parsed HTTP headers. The following is a static stream configuration excerpt; it requires the stream subsystem and its JavaScript extension in addition to the verified module source. js_preread and js_filter are other stream-specific integration points, not HTTP location handlers. Do not assume one network chunk contains a full application message.
stream { js_import session from session.js; js_set $label session.label; server { listen 127.0.0.1:19440; return $label; }}Keep the session logic small
The module below illustrates a fixed stream diagnostic label. It accepts a session object rather than an HTTP request; it makes no external calls and does not inspect payloads. Expected result is the line lab-session. This is intentionally a constant to expose the integration boundary; a normal stream return directive would be simpler in production.
function label(s) { return "lab-session\n";}export default { label };Review lifecycle and operational risks
An event-driven worker can be delayed by CPU-heavy JavaScript or blocking work. Limit input sizes and processing time, avoid per-request filesystem access, and use immutable module-level configuration rather than private request state in reusable contexts. QuickJS context pooling can preserve module-level mutations across invocations; separate requests must not share credentials or private results. Test with the exact engine and package combination. Native extensions, Lua libraries and optional scripting modules each need their own patch and provenance review. A passing Node unit test checks only the ECMAScript logic, not the Nginx integration.
Review checklist:- Known module source and compatible ABI- Bounded input and synchronous work- No client-controlled network destination- No private data in shared state- Actual module syntax and integration tests before releasePractice and next steps
Choose an implementation for a fixed routing table, a small display transformation and a database-backed ordering workflow. Justify the maintenance and isolation boundary. Next: subrequests, internal redirects and response filters (Chapter 40).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Extension boundaries
Compare njs, OpenResty and native modules
What is the simplest tool for a fixed string lookup?
What does the stream extension pass to handlers?
Why avoid private request data in mutable globals?
What is OpenResty?
Where should a complex ordering workflow with database transactions usually live?