[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-07 Thread TimBarham
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To 

[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-07 Thread omefire
Github user omefire commented on the pull request:

https://github.com/apache/cordova-lib/pull/314#issuecomment-146290411
  
LGTM!


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org



[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-07 Thread omefire
Github user omefire commented on a diff in the pull request:

https://github.com/apache/cordova-lib/pull/314#discussion_r41426471
  
--- Diff: cordova-serve/serve.js ---
@@ -17,9 +17,41 @@
  under the License.
  */
 
-module.exports = {
-sendStream: require('./src/stream'),
-servePlatform: require('./src/platform'),
-launchServer: require('./src/server'),
-launchBrowser: require('./src/browser')
+var chalk = require('chalk'),
+compression = require('compression'),
+express = require('express'),
+server = require('./src/server');
+
+module.exports = function () {
+return new CordovaServe();
 };
+
+function CordovaServe() {
+this.app = express();
+
+// Attach this before anything else to provide status output
+this.app.use(function (req, res, next) {
+res.on('finish', function () {
+var color = this.statusCode == '404' ? chalk.red : chalk.green;
+var msg = color(this.statusCode) + ' ' + this.req.originalUrl;
+var encoding = this._headers && 
this._headers['content-encoding'];
+if (encoding) {
+msg += chalk.gray(' (' + encoding + ')');
+}
+server.log(msg);
+});
+next();
+});
+
+// Turn on compression
+this.app.use(compression());
+
+this.servePlatform = require('./src/platform');
+this.launchServer = server;
+}
--- End diff --

+1


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org



[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-07 Thread omefire
Github user omefire commented on a diff in the pull request:

https://github.com/apache/cordova-lib/pull/314#discussion_r41429108
  
--- 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 --

I wonder if 'middleware' is a more familiar term to express users. WDYT ?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org



[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-05 Thread TimBarham
Github user TimBarham commented on the pull request:

https://github.com/apache/cordova-lib/pull/314#issuecomment-145741275
  
It only impacts the `cordova run browser` command (which already runs on a 
local node server) - it has no impact on how users might deploy their app when 
targeting the browser platform.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org



[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-05 Thread TimBarham
Github user TimBarham commented on the pull request:

https://github.com/apache/cordova-lib/pull/314#issuecomment-145742256
  
No problem :smile:


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org



[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-05 Thread purplecabbage
Github user purplecabbage commented on the pull request:

https://github.com/apache/cordova-lib/pull/314#issuecomment-145741933
  
Great, thanks. 
I love express, just wanted to be sure. 


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org



[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-05 Thread asfgit
Github user asfgit closed the pull request at:

https://github.com/apache/cordova-lib/pull/314


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org



[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-05 Thread purplecabbage
Github user purplecabbage commented on the pull request:

https://github.com/apache/cordova-lib/pull/314#issuecomment-145737064
  
How does this impact the cordova-browser platform? Do browser targeting 
apps now depend on express and node on the server? Or can they be deployed as 
static sites?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org



[GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.

2015-10-03 Thread TimBarham
GitHub user TimBarham opened a pull request:

https://github.com/apache/cordova-lib/pull/314

Refactor cordova-serve to use Express.

This simplifies a lot of code, but more importantly provides a more 
standardized and modular way to customize the server (via Express middleware).

This is a breaking change (so next release should be bumped to `0.2.0`).

 API Changes:

* Now call module as a constructor that returns a `cordova-serve` instance. 
This allows creation of multiple servers listening to different ports.
* `launchBrowser()` is a static method of `cordova-serve`
* `servePlatform()` and `launchServer()` are instance methods.
* `sendStream()` method removed (not needed - just use `send` module).
* `opts` parameter loses the following properties: `urlPathHandler`, 
`streamHandler` and `serverExtender`. These can all be done in other ways 
(primarily by attaching Express middleware to the `app` property exposed on the 
`cordova-serve` instance).
* `opts` parameter gains the following properties: `router` (an optional 
Express router that will be attached to server) and `events` (an optional 
`EventEmitter` which will be used for logging - via `events.emit('log', msg)` - 
if provided).


You can merge this pull request into a Git repository by running:

$ git pull https://github.com/MSOpenTech/cordova-lib cordova-serve-express

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/cordova-lib/pull/314.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

This closes #314


commit f140e85f2b68d62f14485b0c1c35afc888e1f05e
Author: Tim Barham 
Date:   2015-10-03T23:34:36Z

Refactor cordova-serve to use Express.

This simplifies a lot of code, and also provides a more standardized and 
modular way to customize the server (via Express middleware).

Also changes cordova-serve to support multiple instances (i.e. multiple 
servers).

Note that this is a breaking change, so for next release we will need to 
update the version to 0.2.0 (and I have updates to cordova-browser and 
cordova-lib ('cordova serve' command) ready to go to make use of this change.




---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

-
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org