cgi-script

1.0.0 • Public • Published

Allows you to run nodejs as Common Gateway Interface (CGI) script by some other process (e.g. apache or another instance of nodejs), rather than acting as a standalone HTTP server, while using an API familiar from the http module. Its main intent is to allow the same application to be run either way.

HTTP request headers are read from process.env and the request body is read from process.stdin. Response headers and body are written to process.stdout. For more information about the Common Gateway interface, see RFC 3875.

This package does the opposite of the cgi package. While the latter allows you to run any CGI script from a nodejs HTTP server, this package allows you to run a nodejs CGI script from any HTTP server.

This package does not provide a full javascript preprocessor as node-cgi does. It intends to do just the minimum to run your applications the usual way, not to provide an entire new way of developing nodejs web applications.

Using cgi-script

Assuming you have a file named helloworld.js:

#!/usr/bin/nodejs

const cgiScript = require('cgi-script');

const server = cgiScript.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen();

Note that the shebang #!/path/to/nodejs is mandatory for most HTTP servers.

To run this using Apache, you could add to your .htaccess file:

<Files helloworld.js>
	SetHandler cgi-script
</Files>

When accessing the URL http://yourserver/path/to/helloworld.js, you would receive a plaintext file containing the text "Hello World".

Package Contents

  • cgiScript.createServer - creates a fake HTTP server
  • cgiScript.Server - a fake HTTP server that serves the CGI request
  • cgiScript.Request - a http.IncomingMessage reading headers from process.env
  • cgiScript.Response - a http.ServerResponse writing the response in CGI format (basically, replaces HTTP 200 OK by Status: 200 OK)
  • cgiScript.Socket - a stream.Duplex reading from process.stdin and writing to process.stdout

Documentation

Additional documentation is provided using jsdoc.

See Also

  • RFC 3875 - The Common Gateway Interface (CGI) Version 1.1
  • http - NodeJS HTTP interface.
  • process - NodeJS process interface.
  • cgi - An http/stack/connect layer to invoke and serve CGI executables.
  • node-cgi - Node as CGI-module. Proof of concept.

Readme

Keywords

Package Sidebar

Install

npm i cgi-script

Weekly Downloads

0

Version

1.0.0

License

GPL-3.0-or-later

Unpacked Size

8.33 kB

Total Files

3

Last publish

Collaborators

  • cmilkau