|
26 | 26 |
|
27 | 27 | 'use strict'; |
28 | 28 |
|
29 | | -var path = require('path') |
30 | | - , fcgiFramework = require('../index.js'); //this we want to test |
| 29 | +var path = require('path'), |
| 30 | + fcgiFramework = require('../index.js'); //this we want to test |
31 | 31 |
|
32 | 32 | var port = 8080; |
33 | 33 | var socketPath = path.join(__dirname, 'echoServer'); |
34 | 34 | try { |
35 | | - require('fs').unlinkSync(socketPath); |
| 35 | + require('fs').unlinkSync(socketPath); |
36 | 36 | } catch (err) { |
37 | | - //ignore if file doesn't exists |
38 | | - if(err.code !== 'ENOENT') { |
39 | | - throw err; |
40 | | - } |
| 37 | + //ignore if file doesn't exists |
| 38 | + if (err.code !== 'ENOENT') { |
| 39 | + throw err; |
| 40 | + } |
41 | 41 | } |
42 | 42 |
|
43 | 43 | function answerWithError(res, err) { |
44 | | - res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8', 'Content-Length': err.stack.length }); |
45 | | - res.end(err.stack + '\n'); |
| 44 | + res.writeHead(500, { |
| 45 | + 'Content-Type': 'text/plain; charset=utf-8', |
| 46 | + 'Content-Length': err.stack.length + 1 |
| 47 | + }); |
| 48 | + |
| 49 | + res.end(err.stack + '\n'); |
46 | 50 | } |
47 | 51 |
|
48 | | -fcgiFramework.createServer( |
49 | | - function echo(req, res) { |
| 52 | +fcgiFramework.createServer(function echo(req, res) { |
50 | 53 | var requestData; |
51 | 54 |
|
52 | 55 | req.on('data', function (data) { |
53 | | - requestData = requestData + data; |
| 56 | + requestData = requestData + data; |
54 | 57 | }); |
55 | 58 |
|
56 | 59 | req.on('complete', function writeReqAsJson() { |
57 | | - var echoData |
58 | | - , size; |
| 60 | + var echoData, size; |
59 | 61 |
|
60 | | - try { |
61 | | - var strippedRequest = require('lodash').omit(req, 'connection', 'buffer', 'socket', '_events', '_readableState', 'data'); |
62 | | - strippedRequest.data = requestData; |
| 62 | + try { |
| 63 | + var strippedRequest = require('lodash').omit(req, 'connection', 'buffer', 'socket', '_events', '_readableState', 'data'); |
| 64 | + strippedRequest.data = requestData; |
63 | 65 |
|
64 | | - echoData = JSON.stringify(strippedRequest, null, 4); //hopefully only here will an error be thrown |
65 | | - size = Buffer.byteLength(echoData, 'utf8'); |
66 | | - res.writeHead( |
67 | | - 200, |
68 | | - { |
69 | | - 'Content-Type': 'application/json; charset=utf-8', |
70 | | - 'Content-Length': size |
71 | | - } |
72 | | - ); |
73 | | - res.end(echoData); |
74 | | - } catch (err) { |
75 | | - answerWithError(res, err); |
76 | | - } |
| 66 | + echoData = JSON.stringify(strippedRequest, null, 4); //hopefully only here will an error be thrown |
| 67 | + size = Buffer.byteLength(echoData, 'utf8'); |
| 68 | + res.writeHead(200, { |
| 69 | + 'Content-Type': 'application/json; charset=utf-8', |
| 70 | + 'Content-Length': size |
| 71 | + }); |
| 72 | + res.end(echoData); |
| 73 | + } catch (err) { |
| 74 | + answerWithError(res, err); |
| 75 | + } |
77 | 76 | }); |
78 | 77 |
|
79 | 78 | req.on('error', answerWithError.bind(undefined, res)); |
80 | | - } |
81 | | -).listen(socketPath, function cgiStarted(err) { |
82 | | - console.log('cgi app listen on socket:' + socketPath); |
83 | | - if (err) { |
84 | | - throw err; |
85 | | - } else { |
86 | | - var http = require('http'); |
87 | | - var fcgiHandler = require('fcgi-handler'); |
| 79 | +}).listen(socketPath, function cgiStarted(err) { |
| 80 | + console.log('cgi app listen on socket:' + socketPath); |
| 81 | + if (err) { |
| 82 | + throw err; |
| 83 | + } else { |
| 84 | + var http = require('http'); |
| 85 | + var fcgiHandler = require('fcgi-handler'); |
88 | 86 |
|
89 | | - var server = http.createServer(function (req, res) { |
90 | | - fcgiHandler.connect({path: socketPath}, function (err, fcgiProcess) { |
91 | | - if (err) { |
92 | | - throw err; |
93 | | - } else { |
94 | | - //route all request to fcgi application |
95 | | - fcgiProcess.handle(req, res, {/*empty Options*/}); |
96 | | - } |
97 | | - }); |
98 | | - }); |
99 | | - server.listen(port); |
100 | | - } |
| 87 | + var server = http.createServer(function (req, res) { |
| 88 | + fcgiHandler.connect({ |
| 89 | + path: socketPath |
| 90 | + }, function (err, fcgiProcess) { |
| 91 | + if (err) { |
| 92 | + throw err; |
| 93 | + } else { |
| 94 | + //route all request to fcgi application |
| 95 | + fcgiProcess.handle(req, res, { /*empty Options*/ }); |
| 96 | + } |
| 97 | + }); |
| 98 | + }); |
| 99 | + server.listen(port); |
| 100 | + } |
101 | 101 | }); |
0 commit comments