Skip to content

Commit 37eb4ef

Browse files
Remove 'express' and 'body-parser' NPM dependencies from HttpNodeInstance
1 parent de991b9 commit 37eb4ef

4 files changed

Lines changed: 39 additions & 41 deletions

File tree

Microsoft.AspNet.NodeServices/Content/Node/entrypoint-http.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
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');
14
var path = require('path');
2-
var express = require('express');
3-
var bodyParser = require('body-parser')
45
var requestedPortOrZero = parseInt(process.argv[2]) || 0; // 0 means 'let the OS decide'
56

67
autoQuitOnFileChange(process.cwd(), ['.js', '.json', '.html']);
78

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 + '"');
2816
}
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+
});
3238
});
3339

34-
var listener = app.listen(requestedPortOrZero, 'localhost', function () {
40+
server.listen(requestedPortOrZero, 'localhost', function () {
3541
// 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 + '\]');
3743

3844
// Signal to the NodeServices base class that we're ready to accept invocations
3945
console.log('[Microsoft.AspNet.NodeServices:Listening]');
4046
});
4147

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)); });
4853
}
4954

5055
function autoQuitOnFileChange(rootDir, extensions) {

samples/angular/MusicStore/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
"dependencies": {
55
"angular2": "2.0.0-alpha.44",
66
"angular2-universal-patched": "^0.5.4",
7-
"body-parser": "^1.14.1",
87
"bootstrap": "^3.3.5",
9-
"del": "^2.0.2",
108
"es6-module-loader": "^0.15.0",
11-
"express": "^4.13.3",
129
"jquery": "^2.1.4",
1310
"less": "^2.5.3",
1411
"reflect-metadata": "^0.1.2",

samples/misc/ES2015Transpilation/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
"name": "ES2015Example",
33
"version": "0.0.0",
44
"dependencies": {
5-
"babel-core": "^5.8.29",
6-
"body-parser": "^1.14.1",
7-
"express": "^4.13.3"
5+
"babel-core": "^5.8.29"
86
}
97
}

samples/react/ReactGrid/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"version": "0.0.0",
44
"dependencies": {
55
"babel-core": "^5.8.29",
6-
"body-parser": "^1.14.1",
76
"bootstrap": "^3.3.5",
8-
"express": "^4.13.3",
97
"griddle-react": "^0.2.14",
108
"history": "^1.12.6",
119
"react": "^0.14.0",

0 commit comments

Comments
 (0)