Skip to content

Conversation

@oliver-moran
Copy link

@oliver-moran oliver-moran commented Apr 22, 2021

Please ensure that your pull request fulfills these requirements:

  • The pull request is being made against the master branch
  • Tests for the changes have been added (for bug fixes / features)

What is the purpose of this pull request? (bug fix, enhancement, new feature,...)

Adds a CGI feature. This enables execution of server-side scripts in a /cgi-bin/ directory using the Common Gateway Interface. Use case is for when small server-side scripting is needed to support static files (e.g. saving form data to a flat-file database, serving a file listing from a directory).

What changes did you make?

Added an interface to the cgi module, with error prevention and usability assumptions (e.g. .js files don't need to be set to executable on Unix).

Provide some example code that this change will affect, if applicable:

Place this file in /public/cgi-bin/ and name it script.js:

console.log('Content-Type: text/plain');
console.log('');
console.log(`Hello, ${process.env.REMOTE_ADDR}!`);

Run http-server with the --cgi option:

$ http-server --cgi

Open http://127.0.0.1:8080/cgi-bin/script.js and the script is executed:

Hello, 127.0.0.1!

Is there anything you'd like reviewers to focus on?

Error handling, especially common mistakes that novice users of CGI would make. One known issue is in the handling of malformed headers by the cgi module. Error handling is not implemented in the latest released version of this module. I've added a pull request to fix this but the module does not appear to be maintained.

Execution of scripts on different operating systems. For JavaScript files (.js), there should be no difference between OSes. These don't need to be set to executable in Unix systems. On Unix, there is better choice of executing scripts in different languages (e.g. Python, bash). However, these need to be set to executable. On Windows, .bat, .cmd and .exe are supported in addition to JavaScript (.js).

Please provide testing instructions, if applicable:

It's a scripting language, so go wild!

@thornjad
Copy link
Member

While I think this is a really neat feature, I'm not so sure this is within the scope of http-server, which is a static content server. Would this be better suited as a companion package which depends on http-server?

@thornjad
Copy link
Member

I'm going to close this due to inactivity and possibly being out of scope for now, we can always re-open later if this is highly desired functionality

@thornjad thornjad closed this Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants