JSGI Middleware Adapter for Node.js
raw JSON →This package, `jsgi-node`, functions as an adapter to execute JSGI (JavaScript Gateway Interface) middleware within the Node.js environment. It specifically implements the JSGI 0.3 specification, which includes support for promise-based asynchronous operations, aligning with Node.js's non-blocking I/O model. JSGI is an asynchronous middleware interface conceived with design principles akin to WSGI in Python or Rack in Ruby, promoting straightforward and efficient middleware connectivity through JavaScript closures. While `jsgi-node` itself does not bundle JSGI components, it provides the runtime necessary for existing JSGI middleware stacks, often found in older projects such as Pintura. The package, currently at version 0.3.3, received its last significant updates over a decade ago. It is considered an abandoned project, primarily of historical interest, and is generally unsuitable for contemporary Node.js application development due to its reliance on an outdated CommonJS draft specification.
Common errors
error Error: Cannot find module 'jsgi-node' ↓
npm install jsgi-node in your project directory. error TypeError: require(...) is not a function ↓
.js file without "type": "module" in package.json) and use const jsgiNode = require('jsgi-node');. error TypeError: request.body.join is not a function ↓
request.body resolves to an iterable suitable for join(). This often implies issues with how input streams are handled or promised. error (node:XXXXX) UnhandledPromiseRejectionWarning: Unhandled promise rejection. ↓
.catch() handlers to explicitly manage errors. Review the promise implementation if promised-io or similar is not explicitly used. Warnings
breaking This package is built for an ancient Node.js environment (likely Node.js 0.x - 0.10) and the CommonJS JSGI 0.3 draft specification. It is fundamentally incompatible with modern Node.js versions (e.g., Node.js 14+) without significant polyfills, transpilation, or direct code modification, especially regarding `Buffer` and stream APIs. ↓
gotcha `jsgi-node` is a CommonJS-only package. Attempting to `import` it in an ES module context will result in errors. Direct interop with modern ESM projects will require bundlers or explicit `createRequire` usage. ↓
deprecated The underlying JSGI specification itself is a deprecated, unmaintained draft standard from the early CommonJS era. There is no active community support or development for JSGI middleware, severely limiting available components and expertise. ↓
breaking The package is abandoned and has not been updated in over a decade. It contains unpatched bugs, security vulnerabilities (CVEs), and design patterns that are likely insecure or inefficient by modern standards. Do not use in production. ↓
gotcha The promise implementation expected by JSGI 0.3 for request bodies and asynchronous responses may not be fully compatible with native `Promise` in modern Node.js, potentially leading to `UnhandledPromiseRejectionWarning` or unexpected behavior if not using a specific A+ compatible library like `promised-io`. ↓
Install
npm install jsgi-node yarn add jsgi-node pnpm add jsgi-node Imports
- start wrong
import { start } from 'jsgi-node'correctconst { start } = require('jsgi-node') - Listener wrong
import { Listener } from 'jsgi-node'correctconst { Listener } = require('jsgi-node') - Node wrong
import { Node } from 'jsgi/node'correctconst { Node } = require('jsgi/node')
Quickstart
const http = require("http");
const jsgiNode = require("jsgi-node");
// Define a simple JSGI application that responds with "Hello World!"
// A JSGI application is a function that takes a request object and returns a promise
// that resolves to a response object.
const myJsgiApp = function(request){
console.log(`[${new Date().toISOString()}] Received request: ${request.method} ${request.url}`);
return Promise.resolve({
status: 200,
headers: { "Content-Type": "text/plain" },
body: ["Hello World from JSGI-Node! Your path was: " + request.pathInfo]
});
};
// Use jsgi-node's Listener to adapt the JSGI app for Node.js's http.createServer
const server = http.createServer(jsgiNode.Listener(myJsgiApp));
const PORT = process.env.PORT ?? 3000;
server.listen(PORT, () => {
console.log(`JSGI-Node server listening on http://localhost:${PORT}`);
console.log("Access the server in your browser or with curl.");
});
// To gracefully shut down the server in a real application
process.on('SIGINT', () => {
console.log('Server shutting down...');
server.close(() => {
console.log('Server gracefully stopped.');
process.exit(0);
});
});