Setup cgi-core
Use this skill to safely and quickly set up cgi-core for CGI script hosting.
Workflow
- Confirm runtime/platform and deployment shape (CLI-only, embedded HTTP server, or framework integration).
- Install
cgi-core. - Configure
urlPath,filePath, and interpreterextensions. - Add operational settings (timeouts, buffers, logging, status pages, env).
- Validate behavior with local requests and security checks.
Required discovery questions
Collect these before editing:
- Package manager (
npm,pnpm,yarn). - OS/runtime target (
Linux/macOS/Windows, Node version). - Hosting mode:
- npx cgi-server CLI - embedded node:http server - integration in existing app/router
- CGI scripts directory path (for example
./cgi-bin). - Languages/interpreters required (
perl,python,node, etc.). - Need for reverse proxy behavior (
trustProxy) and whether proxy is trusted. - Request/response size and timeout expectations.
If details are missing, choose practical defaults and call them out.
Install
Use:
npm install cgi-core --ignore-scriptsFor quick local bootstrapping:
npx cgi-server --filePath ./cgi-binBaseline embedded server
Use this default server shape (ESM):
import { createServer } from "node:http";
import { createHandler } from "cgi-core";
const handler = createHandler({
urlPath: "/cgi-bin",
filePath: "./cgi-bin",
extensions: {
"/usr/bin/perl": ["pl", "cgi"],
"/usr/bin/python": ["py"],
"/usr/local/bin/node": ["js", "node"],
},
debugOutput: false,
});
const app = createServer(async (req, res) => {
const requestHandled = await handler(req, res);
if (!requestHandled) {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("outside of urlPath");
}
});
app.listen(3000);Configuration guidance
Tune these based on user needs:
urlPath: base CGI route, default/cgi-bin.filePath: scripts root; strongly prefer explicit path.extensions: map interpreters to extensions.
- On Windows, recommend absolute interpreter paths for reliability/perf.
indexExtension: extension used for directory index scripts (jsby default).debugOutput: enable only during debugging.logRequests: enable for local troubleshooting.maxBuffer: cap payload sizes for safer resource use.requestChunkSize/responseChunkSize: chunking/perf tradeoff knobs.requestTimeout: set strict timeout to prevent hanging requests.statusPages: custom responses for codes like 404/500.env: static object or updater function per request.trustProxy: enable only behind trusted reverse proxy.
Proxy and security guardrails
When trustProxy is requested:
- Confirm the app is behind a trusted reverse proxy.
- Explain header spoofing risk if exposed directly to the public internet.
- Prefer fixed/validated proxy header handling in infra config.
Always keep filePath constrained to intended CGI directory and avoid dynamic user-controlled script paths.
CLI usage pattern
Use this when the user wants a lightweight standalone server:
npx cgi-server --port 3001 --urlPath /cgi-bin --filePath ./cgi-binCommon flags to mention:
--indexExtension--maxBuffer--requestChunkSize--responseChunkSize--requestTimeout--trustProxy--debugOutput--logRequests
Reverse proxy note
If user asks about Nginx/front proxy:
- Recommend reverse proxy for TLS/static caching/perimeter controls.
- Keep warning that local example configs are not production-safe as-is.
- Emphasize static backend
Hostusage in production to avoid spoofing/cache issues.
Validation checklist
After setup, confirm:
cgi-coreappears in dependencies.- Server starts and listens on expected port.
- A script under
filePathexecutes correctly viaurlPath. - Non-CGI routes still behave as intended.
- Timeouts/buffer limits behave as expected for test payloads.
trustProxyis disabled unless trusted proxy is confirmed.
Note: Whenever you start a server for validation, ensure you stop the process once the check is complete. Do not leave background processes hanging in the user's environment.
Troubleshooting
- 404/route misses:
- Check urlPath and script location under filePath.
- Script not executable:
- Verify interpreter mapping in extensions or script shebang/permissions.
- Large payload errors:
- Increase maxBuffer and review chunk sizes.
- Slow/hanging requests:
- Lower requestTimeout, inspect script runtime and upstream dependencies.
- Incorrect client/protocol metadata:
- Re-check trustProxy assumptions and proxy headers.
Output format
Return results in this structure:
- Install command(s) run.
- Files changed and why.
- Final
createHandler(...)or CLI configuration. - Verification commands/URLs used.
- Security notes (especially
trustProxyand path constraints).