cgi-script
v1.0.0
Published
An http-like API for running nodejs as CGI application
Downloads
5
Readme
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 servercgiScript.Server
- a fake HTTP server that serves the CGI requestcgiScript.Request
- ahttp.IncomingMessage
reading headers fromprocess.env
cgiScript.Response
- ahttp.ServerResponse
writing the response in CGI format (basically, replacesHTTP 200 OK
byStatus: 200 OK
)cgiScript.Socket
- astream.Duplex
reading fromprocess.stdin
and writing toprocess.stdout
Documentation
Additional documentation is provided using jsdoc.