{"id":17659,"library":"gateway","title":"Node.js CGI Middleware","description":"gateway is a Node.js middleware designed to facilitate the execution of CGI scripts, such as PHP files, within a Node.js HTTP server environment. It was initially developed around 2012 to support development tools like Yeoman and Livestyle in serving dynamic content. The package's current stable version is 1.0.0, released over a decade ago, indicating it is no longer actively maintained. Unlike `node-cgi`, `gateway` focuses on serving individual script files rather than `cgi-bin` directories, requiring the corresponding CGI binary (e.g., `php-cgi`) to be installed and accessible in the system's PATH. This approach allows Node.js applications to integrate with existing CGI-based applications but comes with the inherent security and maintenance implications of executing external processes.","status":"abandoned","version":"1.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/fgnass/gateway","tags":["javascript","connect","middleware","cgi","php"],"install":[{"cmd":"npm install gateway","lang":"bash","label":"npm"},{"cmd":"yarn add gateway","lang":"bash","label":"yarn"},{"cmd":"pnpm add gateway","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS module and must be imported using `require()`. It does not support native ESM `import` statements, nor does it provide TypeScript type definitions.","wrong":"import gateway from 'gateway';","symbol":"gateway","correct":"const gateway = require('gateway');"}],"quickstart":{"code":"const http = require('http');\nconst gateway = require('gateway');\nconst path = require('path');\nconst fs = require('fs');\n\n// Create a temporary directory and a PHP file for demonstration\nconst tempDir = path.join(__dirname, 'temp_gateway_test');\nif (!fs.existsSync(tempDir)) {\n  fs.mkdirSync(tempDir);\n}\nconst phpFilePath = path.join(tempDir, 'info.php');\nfs.writeFileSync(phpFilePath, `<?php phpinfo(); ?>`);\n\nconsole.log('Created temporary PHP file at:', phpFilePath);\nconsole.log('Ensure php-cgi is installed and in your PATH for this example to work.');\n\nconst server = http.createServer(gateway(tempDir, {\n  '.php': 'php-cgi'\n}));\n\nconst PORT = 3000;\nserver.listen(PORT, () => {\n  console.log(`Gateway server listening on http://localhost:${PORT}`);\n  console.log(`Try visiting http://localhost:${PORT}/info.php`);\n  console.log(`Press Ctrl+C to stop the server and clean up.`);\n});\n\n// Basic cleanup on exit\nprocess.on('SIGINT', () => {\n  console.log('\\nShutting down server...');\n  server.close(() => {\n    if (fs.existsSync(phpFilePath)) {\n      fs.unlinkSync(phpFilePath);\n    }\n    if (fs.existsSync(tempDir)) {\n      fs.rmdirSync(tempDir);\n    }\n    console.log('Cleaned up temporary files. Server stopped.');\n    process.exit(0);\n  });\n});","lang":"javascript","description":"This example sets up a basic Node.js HTTP server using `gateway` to serve a dynamically created `info.php` file, demonstrating how to execute PHP scripts through `php-cgi`."},"warnings":[{"fix":"Migrate to actively maintained alternatives or use more secure methods for serving dynamic content. If absolutely necessary, thoroughly audit any external scripts executed.","message":"This package is considered abandoned and has not been updated in over 10 years. It is highly susceptible to unpatched security vulnerabilities, especially when executing external CGI scripts. Use in production or with untrusted input is strongly discouraged.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"If `cgi-bin` functionality is required, consider using alternative packages like `node-cgi` or setting up a dedicated web server (e.g., Nginx, Apache) to handle `cgi-bin`.","message":"`gateway` explicitly does not support `cgi-bin` directories for script execution. Its design focuses on serving individual script files based on file extension mapping.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Install the necessary CGI binaries for your scripting language (e.g., `brew install php@XX` for PHP on macOS) and ensure their location is included in your system's PATH.","message":"The middleware requires external CGI executables (e.g., `php-cgi`) to be pre-installed on the system and accessible in the system's PATH environment variable. Failure to do so will prevent scripts from executing correctly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be aware of this redirect behavior in your application's routing. Ensure client requests for directories include a trailing slash if redirects are undesired.","message":"When a URL points to a valid directory but does not end with a trailing slash, `gateway` will issue a permanent (301) redirect to append the slash. This behavior may impact routing or caching strategies.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use `const gateway = require('gateway');` for importing. For TypeScript projects, you may need to create a custom declaration file (`.d.ts`) if type safety is desired.","message":"This package is a CommonJS module and does not natively support ES Modules (ESM) `import` syntax. It also does not ship with TypeScript type definitions.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Install the required CGI binary and ensure its installation directory is correctly added to your system's PATH environment variable.","cause":"The specified CGI executable (e.g., `php-cgi`) cannot be found in the system's PATH.","error":"Error: spawn <cgi-binary-name> ENOENT"},{"fix":"Verify that the `gateway` options correctly map the file extension (e.g., `'.php': 'php-cgi'`) and confirm that the CGI binary (e.g., `php-cgi`) is installed and can execute the script from the command line.","cause":"`gateway` is serving the PHP file content directly instead of executing it, likely due to a misconfiguration in the file extension mapping or the `php-cgi` binary not functioning.","error":"PHP script content displayed as plain text in the browser."},{"fix":"Use the CommonJS `require` syntax: `const gateway = require('gateway');`.","cause":"Attempting to import the CommonJS `gateway` module using an ESM `import` statement in an ESM context, leading to an incorrect module resolution.","error":"TypeError: gateway is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}