mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 01:58:29 +00:00
Remove 'express' and 'body-parser' NPM dependencies from HttpNodeInstance
This commit is contained in:
@@ -1,19 +1,18 @@
|
|||||||
|
// Limit dependencies to core Node modules. This means the code in this file has to be very low-level and unattractive,
|
||||||
|
// but simplifies things for the consumer of this module.
|
||||||
|
var http = require('http');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var express = require('express');
|
|
||||||
var bodyParser = require('body-parser')
|
|
||||||
var requestedPortOrZero = parseInt(process.argv[2]) || 0; // 0 means 'let the OS decide'
|
var requestedPortOrZero = parseInt(process.argv[2]) || 0; // 0 means 'let the OS decide'
|
||||||
|
|
||||||
autoQuitOnFileChange(process.cwd(), ['.js', '.json', '.html']);
|
autoQuitOnFileChange(process.cwd(), ['.js', '.json', '.html']);
|
||||||
|
|
||||||
var app = express();
|
var server = http.createServer(function(req, res) {
|
||||||
app.use(bodyParser.json());
|
readRequestBodyAsJson(req, function(bodyJson) {
|
||||||
|
var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
|
||||||
app.all('/', function (req, res) {
|
|
||||||
var resolvedPath = path.resolve(process.cwd(), req.body.moduleName);
|
|
||||||
var invokedModule = require(resolvedPath);
|
var invokedModule = require(resolvedPath);
|
||||||
var func = req.body.exportedFunctionName ? invokedModule[req.body.exportedFunctionName] : invokedModule;
|
var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
|
||||||
if (!func) {
|
if (!func) {
|
||||||
throw new Error('The module "' + resolvedPath + '" has no export named "' + req.body.exportedFunctionName + '"');
|
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
|
||||||
}
|
}
|
||||||
|
|
||||||
var hasSentResult = false;
|
var hasSentResult = false;
|
||||||
@@ -22,29 +21,35 @@ app.all('/', function (req, res) {
|
|||||||
hasSentResult = true;
|
hasSentResult = true;
|
||||||
if (errorValue) {
|
if (errorValue) {
|
||||||
res.status(500).send(errorValue);
|
res.status(500).send(errorValue);
|
||||||
|
} else if (typeof successValue === 'object') {
|
||||||
|
// Arbitrary object - JSON-serialize it
|
||||||
|
res.setHeader('Content-Type', 'application/json');
|
||||||
|
res.end(JSON.stringify(successValue));
|
||||||
} else {
|
} else {
|
||||||
sendResult(res, successValue);
|
// String - can bypass JSON-serialization altogether
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.end(successValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
func.apply(null, [callback].concat(req.body.args));
|
func.apply(null, [callback].concat(bodyJson.args));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
var listener = app.listen(requestedPortOrZero, 'localhost', function () {
|
server.listen(requestedPortOrZero, 'localhost', function () {
|
||||||
// Signal to HttpNodeHost which port it should make its HTTP connections on
|
// Signal to HttpNodeHost which port it should make its HTTP connections on
|
||||||
console.log('[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port ' + listener.address().port + '\]');
|
console.log('[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]');
|
||||||
|
|
||||||
// Signal to the NodeServices base class that we're ready to accept invocations
|
// Signal to the NodeServices base class that we're ready to accept invocations
|
||||||
console.log('[Microsoft.AspNet.NodeServices:Listening]');
|
console.log('[Microsoft.AspNet.NodeServices:Listening]');
|
||||||
});
|
});
|
||||||
|
|
||||||
function sendResult(response, result) {
|
function readRequestBodyAsJson(request, callback) {
|
||||||
if (typeof result === 'object') {
|
var requestBodyAsString = '';
|
||||||
response.json(result);
|
request
|
||||||
} else {
|
.on('data', function(chunk) { requestBodyAsString += chunk; })
|
||||||
response.send(result);
|
.on('end', function() { callback(JSON.parse(requestBodyAsString)); });
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function autoQuitOnFileChange(rootDir, extensions) {
|
function autoQuitOnFileChange(rootDir, extensions) {
|
||||||
|
|||||||
@@ -4,11 +4,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"angular2": "2.0.0-alpha.44",
|
"angular2": "2.0.0-alpha.44",
|
||||||
"angular2-universal-patched": "^0.5.4",
|
"angular2-universal-patched": "^0.5.4",
|
||||||
"body-parser": "^1.14.1",
|
|
||||||
"bootstrap": "^3.3.5",
|
"bootstrap": "^3.3.5",
|
||||||
"del": "^2.0.2",
|
|
||||||
"es6-module-loader": "^0.15.0",
|
"es6-module-loader": "^0.15.0",
|
||||||
"express": "^4.13.3",
|
|
||||||
"jquery": "^2.1.4",
|
"jquery": "^2.1.4",
|
||||||
"less": "^2.5.3",
|
"less": "^2.5.3",
|
||||||
"reflect-metadata": "^0.1.2",
|
"reflect-metadata": "^0.1.2",
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
"name": "ES2015Example",
|
"name": "ES2015Example",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babel-core": "^5.8.29",
|
"babel-core": "^5.8.29"
|
||||||
"body-parser": "^1.14.1",
|
|
||||||
"express": "^4.13.3"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,7 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babel-core": "^5.8.29",
|
"babel-core": "^5.8.29",
|
||||||
"body-parser": "^1.14.1",
|
|
||||||
"bootstrap": "^3.3.5",
|
"bootstrap": "^3.3.5",
|
||||||
"express": "^4.13.3",
|
|
||||||
"griddle-react": "^0.2.14",
|
"griddle-react": "^0.2.14",
|
||||||
"history": "^1.12.6",
|
"history": "^1.12.6",
|
||||||
"react": "^0.14.0",
|
"react": "^0.14.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user