|
| 1 | +// Limit dependencies to core Node modules. This means the code in this file has to be very low-level and unattractive, |
| 2 | +// but simplifies things for the consumer of this module. |
| 3 | +var http = require('http'); |
1 | 4 | var path = require('path'); |
2 | | -var express = require('express'); |
3 | | -var bodyParser = require('body-parser') |
4 | 5 | var requestedPortOrZero = parseInt(process.argv[2]) || 0; // 0 means 'let the OS decide' |
5 | 6 |
|
6 | 7 | autoQuitOnFileChange(process.cwd(), ['.js', '.json', '.html']); |
7 | 8 |
|
8 | | -var app = express(); |
9 | | -app.use(bodyParser.json()); |
10 | | - |
11 | | -app.all('/', function (req, res) { |
12 | | - var resolvedPath = path.resolve(process.cwd(), req.body.moduleName); |
13 | | - var invokedModule = require(resolvedPath); |
14 | | - var func = req.body.exportedFunctionName ? invokedModule[req.body.exportedFunctionName] : invokedModule; |
15 | | - if (!func) { |
16 | | - throw new Error('The module "' + resolvedPath + '" has no export named "' + req.body.exportedFunctionName + '"'); |
17 | | - } |
18 | | - |
19 | | - var hasSentResult = false; |
20 | | - var callback = function(errorValue, successValue) { |
21 | | - if (!hasSentResult) { |
22 | | - hasSentResult = true; |
23 | | - if (errorValue) { |
24 | | - res.status(500).send(errorValue); |
25 | | - } else { |
26 | | - sendResult(res, successValue); |
27 | | - } |
| 9 | +var server = http.createServer(function(req, res) { |
| 10 | + readRequestBodyAsJson(req, function(bodyJson) { |
| 11 | + var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName); |
| 12 | + var invokedModule = require(resolvedPath); |
| 13 | + var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule; |
| 14 | + if (!func) { |
| 15 | + throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"'); |
28 | 16 | } |
29 | | - }; |
30 | | - |
31 | | - func.apply(null, [callback].concat(req.body.args)); |
| 17 | + |
| 18 | + var hasSentResult = false; |
| 19 | + var callback = function(errorValue, successValue) { |
| 20 | + if (!hasSentResult) { |
| 21 | + hasSentResult = true; |
| 22 | + if (errorValue) { |
| 23 | + res.status(500).send(errorValue); |
| 24 | + } else if (typeof successValue === 'object') { |
| 25 | + // Arbitrary object - JSON-serialize it |
| 26 | + res.setHeader('Content-Type', 'application/json'); |
| 27 | + res.end(JSON.stringify(successValue)); |
| 28 | + } else { |
| 29 | + // String - can bypass JSON-serialization altogether |
| 30 | + res.setHeader('Content-Type', 'text/plain'); |
| 31 | + res.end(successValue); |
| 32 | + } |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + func.apply(null, [callback].concat(bodyJson.args)); |
| 37 | + }); |
32 | 38 | }); |
33 | 39 |
|
34 | | -var listener = app.listen(requestedPortOrZero, 'localhost', function () { |
| 40 | +server.listen(requestedPortOrZero, 'localhost', function () { |
35 | 41 | // Signal to HttpNodeHost which port it should make its HTTP connections on |
36 | | - console.log('[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port ' + listener.address().port + '\]'); |
| 42 | + console.log('[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]'); |
37 | 43 |
|
38 | 44 | // Signal to the NodeServices base class that we're ready to accept invocations |
39 | 45 | console.log('[Microsoft.AspNet.NodeServices:Listening]'); |
40 | 46 | }); |
41 | 47 |
|
42 | | -function sendResult(response, result) { |
43 | | - if (typeof result === 'object') { |
44 | | - response.json(result); |
45 | | - } else { |
46 | | - response.send(result); |
47 | | - } |
| 48 | +function readRequestBodyAsJson(request, callback) { |
| 49 | + var requestBodyAsString = ''; |
| 50 | + request |
| 51 | + .on('data', function(chunk) { requestBodyAsString += chunk; }) |
| 52 | + .on('end', function() { callback(JSON.parse(requestBodyAsString)); }); |
48 | 53 | } |
49 | 54 |
|
50 | 55 | function autoQuitOnFileChange(rootDir, extensions) { |
|
0 commit comments