├── LICENSE ├── cgi.js ├── package.json └── readme.md /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2010 Brian McKenna 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /cgi.js: -------------------------------------------------------------------------------- 1 | var sys = require('sys'), 2 | http = require('http'); 3 | 4 | var Request = function() { 5 | this.method = process.env['REQUEST_METHOD']; 6 | this.headers = { 7 | 'host': process.env['HTTP_HOST'], 8 | 'user-agent': process.env['HTTP_USER_AGENT'] 9 | }; 10 | this.url = process.env['REQUEST_URI']; 11 | }; 12 | 13 | var Response = function() { 14 | var body = false; 15 | 16 | this.writeHead = function() { 17 | var status = arguments[0]; 18 | var reason = arguments[1]; 19 | var headers = arguments[2]; 20 | 21 | if (typeof reason != 'string') { 22 | headers = reason; 23 | reason = http.STATUS_CODES[arguments[0]] || 'unknown'; 24 | } 25 | 26 | sys.puts('Status: ' + status + ' ' + reason); 27 | 28 | var field, value; 29 | var keys = Object.keys(headers); 30 | var isArray = (headers instanceof Array); 31 | 32 | for (var i = 0, l = keys.length; i < l; i++) { 33 | var key = keys[i]; 34 | 35 | if (isArray) { 36 | field = headers[key][0]; 37 | value = headers[key][1]; 38 | } else { 39 | field = key; 40 | value = headers[key]; 41 | } 42 | 43 | sys.puts(field + ": " + value); 44 | } 45 | }; 46 | 47 | this.write = function(message) { 48 | if (!body) { 49 | body = true; 50 | sys.puts(""); 51 | } 52 | 53 | if (message) sys.print(message); 54 | }; 55 | 56 | this.flush = function() { 57 | }; 58 | 59 | this.end = function() { 60 | this.write.apply(this, arguments); 61 | }; 62 | }; 63 | 64 | var Server = function(listener, options) { 65 | var request = new Request(); 66 | var response = new Response(); 67 | 68 | this.listen = function() { 69 | listener(request, response); 70 | }; 71 | }; 72 | 73 | exports.createServer = function(listener, options) { 74 | return new Server(listener, options); 75 | }; 76 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-cgi", 3 | "version": "0.1.1", 4 | "author": "Brian McKenna ", 5 | "description": "CGI adapter for node.js for servers that must use CGI", 6 | "homepage": "http://brianmckenna.org/blog/nodejs_via_cgi", 7 | "contributors": [ 8 | "Brian McKenna (http://brianmckenna.org/)", 9 | "Tyler Akins (http://rumkin.com/)" 10 | ], 11 | "main": "./cgi.js", 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/pufuwozu/node-cgi.git" 15 | }, 16 | "keywords": [ 17 | "cgi" 18 | ], 19 | "dependencies": {}, 20 | "devDependencies": {}, 21 | "license": "MIT", 22 | "engines": { 23 | "node": "~0.6", 24 | "npm": "~1" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # node-cgi 2 | 3 | This library is a CGI adaptor for node.js 4 | 5 | It is designed to help people run node.js websites off of a shared server (CGI is so old it's available almost *everywhere*). 6 | 7 | ## Usage 8 | 9 | You'll have to create a `.htaccess` file that rewrites everything to a CGI script: 10 | 11 | Options +ExecCGI 12 | AddHandler cgi-script cgi 13 | 14 | RewriteEngine on 15 | RewriteCond %{REQUEST_FILENAME} !-f 16 | RewriteRule (.*) server.cgi 17 | 18 | Copy `cgi.js` to the same directory and make the actual CGI script (`server.cgi` in this example): 19 | 20 | #!/usr/bin/env node 21 | 22 | var cgi = require('./cgi'); 23 | 24 | var server = cgi.createServer(function(request, response) { 25 | response.writeHead(200, {'Content-Type': 'text/plain'}); 26 | response.write('This is CGI!'); 27 | response.end(); 28 | }); 29 | server.listen(); 30 | 31 | As you can see, using the `cgi` library is very similar to the `http` library in node.js 32 | 33 | ## License 34 | 35 | The `cgi.js` library is released under an MIT license. See `LICENSE` for the full text of the license. 36 | --------------------------------------------------------------------------------