cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From omefire <...@git.apache.org>
Subject [GitHub] cordova-lib pull request: Refactor cordova-serve to use Express.
Date Wed, 07 Oct 2015 18:43:40 GMT
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


Mime
View raw message