接上一篇文章,单独来讨论一下main函数里的信息
var main = function (args, readyCb, doneCb) {
if (args.showConfig) {
try {
console.log(JSON.stringify(getConfig()));
} catch (e) {
process.exit(1);
}
process.exit(0);
}
首先根据参数里的showConfig属性来判断是否需要打印配置信息。
checkArgs(parser, args);
if (typeof doneCb === "undefined") {
doneCb = function () {};
}
然后会检查参数以及判断doneCb是否已经初始化,如果没有初始化,就要为其初始化。
var rest = express()
, server = http.createServer(rest);
然后创建express服务器。
rest.use(domainMiddleware());
rest.use(morgan(function (tokens, req, res) {
// morgan output is redirected straight to winston
logger.info(requestEndLoggingFormat(tokens, req, res),
(res.jsonResp || '').grey);
}));
rest.use(favicon(path.join(__dirname, 'static/favicon.ico')));
rest.use(express.static(path.join(__dirname, 'static')));
rest.use(allowCrossDomain);
rest.use(parserWrap);
rest.use(bodyParser.urlencoded({extended: true}));
// 8/18/14: body-parser requires that we supply the limit field to ensure the server can
// handle requests large enough for Appium's use cases. Neither Node nor HTTP spec defines a max
// request size, so any hard-coded request-size limit is arbitrary. Units are in bytes (ie "gb" == "GB",
// not "Gb"). Using 1GB because..., well because it's arbitrary and 1GB is sufficiently large for 99.99%
// of testing scenarios while still providing an upperbounds to reduce the odds of squirrelliness.
rest.use(bodyParser.json({limit: '1gb'}));
rest.use(morgan(function (tokens, req, res) {
// morgan output is redirected straight to winston
var data = '';
try {
if (req.body) data = JSON.stringify(req.body).substring(0, 1000);
} catch (ign) {}
logger.info(requestStartLoggingFormat(tokens, req, res), data.grey);
}, {immediate: true}));
rest.use(methodOverride());
// Instantiate the appium instance
var appiumServer = appium(args);
// Hook up REST http interface
appiumServer.attachTo(rest);
routing(appiumServer);
rest.use(catchAllHandler);
然后通过express中use方法来加载处理http请求的中间组件,你定义的顺序就是处理的顺序。其中夹杂着一个routing(appiumServer);
语句,大概也是为appium服务器准备好路由器控制器。
async.series([
function (cb) {
configureServer(getConfig(), appiumVer, appiumServer, function (err, rev) {
if (err) return cb(err);
appiumRev = rev;
cb();
});
},
function (cb) {
prepareTmpDir(args, cb);
},
function (cb) {
conditionallyPreLaunch(args, appiumServer, cb);
},
function (cb) {
startListening(server, args, parser, appiumVer, appiumRev, appiumServer, cb);
}
], function (err) {
if (err) {
process.exit(1);
} else if (typeof readyCb === "function") {
readyCb(appiumServer);
}
});
async.series将异步变更为同步,[]包裹的函数,执行的时候会等前一个函数执行完,再执行下一个。
server.on('close', function () {
logFinalWarning();
doneCb();
});
添加close事件,关闭服务的时候,会调用设置好的回掉函数。
总结
这两篇文章只是大致了解下appium程序启动开始的整体工作,那么具体的工作原理还要一步一步深入了解后才好给予跟具体的解释。