Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v0.0.1
Fetching contributors…

Cannot retrieve contributors at this time

55 lines (35 sloc) 1.538 kb

node-cgi

A stack/connect layer to invoke and serve CGI executables.

This module implements RFC 3875, and offers an easy interface to run and serve CGI executables using Node's HTTP server. I wrote this so I could directly serve GitWeb through Node.

CGI Scripts?

If you're not familiar with CGI scripts, they're simply executables files that get invoked by a web server with client requests. The script has Environment Variables set that indicate information about the HTTP request the client has sent.

Here's what a simple "Hello World" CGI script in sh would look like:

#!/bin/sh

# Headers are written first. The special "Status" headers
# indicates the response status code
echo "Status: 200"
echo "Content-Type: text/plain"
echo

# Followed by a response body
echo "Hello World!"

Let's call it hello.cgi. Be sure to make it executable with chmod +x hello.cgi!

Invoking "The Script" with Node

Now, we need to set up our Node HTTP server. For every request sent to the server, our hello.cgi script will be invoked, and the response will be sent back to the HTTP client:

var http = require('http');
var cgi = require('cgi');

http.createServer(
  cgi('hello.cgi')
).listen(80);

This will set up a CGI handler with the default options.

Jump to Line
Something went wrong with that request. Please try again.