Github user TimBarham commented on a diff in the pull request:
https://github.com/apache/cordova-lib/pull/314#discussion_r41439142
--- Diff: cordova-serve/src/server.js ---
@@ -17,121 +17,51 @@
under the License.
*/
-var chalk = require('chalk'),
- fs = require('fs'),
- http = require('http'),
- url = require('url'),
- path = require('path'),
- Q = require('q');
+var chalk = require('chalk'),
+ express = require('express'),
+ Q = require('q');
/**
* @desc Launches a server with the specified options and optional custom
handlers.
- * @param {{root: ?string, port: ?number, noLogOutput: ?bool,
noServerInfo: ?bool, urlPathHandler: ?function, streamHandler: ?function,
serverExtender: ?function}} opts
- * urlPathHandler(urlPath, request, response, do302, do404, serveFile)
- an optional method to provide custom handling for
- * processing URLs and serving up the resulting data. Can serve up
the data itself using response.write(), or determine
- * a custom local file path and call serveFile to serve it up, or
do no processing and call serveFile with no params to
- * treat urlPath as relative to the root.
- * streamHandler(filePath, request, response) - an optional custom
stream handler, to stream whatever you want. If not
- * provided, the file referenced by filePath will be streamed.
Return true if the file is handled.
- * serverExtender(server, root) - if provided, is called as soon as
server is created so caller can attached to events etc.
+ * @param {{root: ?string, port: ?number, noLogOutput: ?bool,
noServerInfo: ?bool, router: ?express.Router, events: EventEmitter}} opts
* @returns {*|promise}
*/
module.exports = function (opts) {
var deferred = Q.defer();
opts = opts || {};
- var root = opts.root;
var port = opts.port || 8000;
- var log = module.exports.log = function () {
+ var log = module.exports.log = function (msg) {
if (!opts.noLogOutput) {
- console.log.apply(console, arguments);
+ if (opts.events) {
+ opts.events.emit('log', msg);
+ } else {
+ console.log(msg);
+ }
}
};
- var server = http.createServer(function (request, response) {
- function do404() {
- log(chalk.red('404 ') + request.url);
- response.writeHead(404, {'Content-Type': 'text/plain'});
- response.write('404 Not Found\n');
- response.end();
- }
-
- function do302(where) {
- log(chalk.green('302 ') + request.url);
- response.setHeader('Location', where);
- response.writeHead(302, {'Content-Type': 'text/plain'});
- response.end();
- }
-
- function do304() {
- log(chalk.green('304 ') + request.url);
- response.writeHead(304, {'Content-Type': 'text/plain'});
- response.end();
- }
-
- function isFileChanged(path) {
- var mtime = fs.statSync(path).mtime,
- itime = request.headers['if-modified-since'];
- return !itime || new Date(mtime) > new Date(itime);
- }
-
- var urlPath = url.parse(request.url).pathname;
+ var app = this.app;
+ var server = require('http').Server(app);
+ this.server = server;
- if (opts.urlPathHandler) {
- opts.urlPathHandler(urlPath, request, response, do302, do404,
serveFile);
- } else {
- serveFile();
- }
-
- function serveFile(filePath) {
- if (!filePath) {
- if (!root) {
- throw new Error('No server root directory HAS BEEN
specified!');
- }
- filePath = path.join(root, urlPath);
- }
+ if (opts.router) {
+ app.use(opts.router);
--- End diff --
"Router" is definitely a term that express users would be familiar with.
While you could pass any middleware in as this option, the more likely scenario
is to pass in a router (which is like a "mini-app" - it provides a single entry
point for a while collection of middleware).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]