|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright (c) 2016 Fabio Massaioli and other contributors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * this software and associated documentation files (the "Software"), to deal in |
| 8 | + * the Software without restriction, including without limitation the rights to |
| 9 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | + * subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +// To run this example: |
| 25 | +// > npm install puffnfresh/node-cgi |
| 26 | +// > ./cgiFallback.js 2> /dev/null |
| 27 | + |
| 28 | +'use strict'; |
| 29 | + |
| 30 | +var fcgi = require('../index.js'), |
| 31 | + cgi = require('node-cgi'), |
| 32 | + fs = require('fs'); |
| 33 | + |
| 34 | +function log(msg) { |
| 35 | + fs.appendFileSync('cgiFallback.log', msg); |
| 36 | +} |
| 37 | + |
| 38 | +var createServer = fcgi.createServer; |
| 39 | + |
| 40 | +// stat fd 0; if it's not a socket switch to plain CGI. |
| 41 | +if (!(fs.fstatSync(0).mode & fs.constants.S_IFSOCK)) |
| 42 | + createServer = cgi.createServer; |
| 43 | + |
| 44 | +createServer(function (req, res) { |
| 45 | + res.writeHead(200, { |
| 46 | + 'Content-Type': 'text/plain', |
| 47 | + }); |
| 48 | + res.end("It's working!\n"); |
| 49 | +}).listen(function () { |
| 50 | + log('Listening\n'); |
| 51 | +}); |
| 52 | + |
| 53 | +process.on('uncaughtException', function (err) { |
| 54 | + log(err.stack + '\n\n'); |
| 55 | + process.exit(1); |
| 56 | +}); |
| 57 | + |
| 58 | +process.on('exit', function () { |
| 59 | + log('Exit - Uptime:' + process.uptime() + '\n\n'); |
| 60 | +}); |
| 61 | + |
| 62 | +process.on('SIGTERM', function () { |
| 63 | + log('SIGTERM\n'); |
| 64 | + process.exit(0); |
| 65 | +}); |
| 66 | + |
| 67 | +process.on('SIGINT', function () { |
| 68 | + log('SIGINT\n'); |
| 69 | + process.exit(0); |
| 70 | +}); |
| 71 | + |
| 72 | +process.on('SIGUSR1', function () { |
| 73 | + log('SIGUSR1\n'); |
| 74 | +}); |
0 commit comments