routing.js是一个路由器模块,至于什么是路由器模块,我们分析完该文件后再给与一个准确的定义,现在我也不太理解,一起学习吧
源码
"use strict";
var controller = require('./controller.js');
module.exports = function (appium) {
var rest = appium.rest;
var globalBeforeFilter = controller.getGlobalBeforeFilter(appium);
// Make appium available to all REST http requests.
rest.all('/wd/*', globalBeforeFilter);
routeNotYetImplemented(rest);
rest.all('/wd/hub/session/*', controller.sessionBeforeFilter);
rest.get('/wd/hub/status', controller.getStatus);
rest.post('/wd/hub/session', controller.createSession);
rest.get('/wd/hub/session/:sessionId?', controller.getSession);
rest.delete('/wd/hub/session/:sessionId?', controller.deleteSession);
rest.get('/wd/hub/sessions', controller.getSessions);
rest.get('/wd/hub/session/:sessionId?/context', controller.getCurrentContext);
rest.post('/wd/hub/session/:sessionId?/context', controller.setContext);
rest.get('/wd/hub/session/:sessionId?/contexts', controller.getContexts);
rest.post('/wd/hub/session/:sessionId?/element', controller.findElement);
rest.post('/wd/hub/session/:sessionId?/elements', controller.findElements);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/value', controller.setValue);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/click', controller.doClick);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/text', controller.getText);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/displayed', controller.elementDisplayed);
......
控制器
首先我们看到加载了controller模块,加载的是本地模块./controller.js
(具体是什么我们以后再说),暂时我们就理解为一些回调函数的集合,这些回调函数是干什么的呢?是在express服务器中用于指定不同的访问路径所对应的回调函数。
rest
这个rest就是express框架的模块实例。为什么这么说呢,我们一步一步来分析,我们在main.js模块中看到下面两句代码:
A代码块:
// Instantiate the appium instance
var appiumServer = appium(args);
// Hook up REST http interface
appiumServer.attachTo(rest);
routing(appiumServer);
然后appium构造函数实际上调用的是appium.js中的
B代码块:
var Appium = function (args) {
...
回调函数,所以appiumServer这个实例实际上就是Appium对象,然后调用routing(appiumServer)自然就是讲appium对象传导routing.js模块中了。所以我们在routing.js模块中找到rest就行了。在上面的A代码块中还有一句appiumServer.attachTo(rest);
而且我们已经知道了appiumServer就是appium.js模块,那么我们就来看看appium.js中得attachTo方法:
Appium.prototype.attachTo = function (rest) {
this.rest = rest;
};
刚好这个方法就是将main.js中的rest(也就是express模块)设置到appium.js中this.rest。所以我们在routing.js中调用的appium.rest就是express模块。那么这个模块中的get/post等等方法,那肯定是自带的喽。
RESTful
这里又要提这个风格,因为要讲明白这个express模块,必须得说说这个RESTFul风格。简单的介绍一下其api
GET(SELECT): 从服务器取出资源(一个或多个)
POST(CREATE): 在服务器新建一个资源
PUT(UPDATE): 在服务器更新资源(客户端提供改变后的完整资源,即更新整一个资源)
PATCH(UPDATE): 在服务器更新资源(客户端提供改变的属性,即更新资源的部分属性)
DELETE(DELETE): 从服务器删除资源
上面的了解了,你也就大致了解了下面的方法的大致含义,比如说
rest.get('/wd/hub/status', controller.getStatus);
从服务器中获取信息,该信息从哪里获得,当时是调用了controller.js模块中的getStatus来获得。
rest.all('/wd/*', globalBeforeFilter);
routeNotYetImplemented(rest);
rest.all('/wd/hub/session/*', controller.sessionBeforeFilter);
rest.get('/wd/hub/status', controller.getStatus);
rest.post('/wd/hub/session', controller.createSession);
rest.get('/wd/hub/session/:sessionId?', controller.getSession);
rest.delete('/wd/hub/session/:sessionId?', controller.deleteSession);
rest.get('/wd/hub/sessions', controller.getSessions);
rest.get('/wd/hub/session/:sessionId?/context', controller.getCurrentContext);
rest.post('/wd/hub/session/:sessionId?/context', controller.setContext);
rest.get('/wd/hub/session/:sessionId?/contexts', controller.getContexts);
rest.post('/wd/hub/session/:sessionId?/element', controller.findElement);
rest.post('/wd/hub/session/:sessionId?/elements', controller.findElements);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/value', controller.setValue);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/click', controller.doClick);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/text', controller.getText);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/displayed', controller.elementDisplayed);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/enabled', controller.elementEnabled);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/selected', controller.elementSelected);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/location', controller.getLocation);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/location_in_view', controller.getLocationInView);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/size', controller.getSize);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/pageIndex', controller.getPageIndex);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/attribute/:name', controller.getAttribute);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/css/:propertyName', controller.getCssProperty);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/equals/:otherId', controller.equalsElement);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?/name', controller.getName);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/clear', controller.clear);
rest.post('/wd/hub/session/:sessionId?/frame', controller.frame);
rest.post('/wd/hub/session/:sessionId?/keys', controller.keys);
rest.post('/wd/hub/session/:sessionId?/location', controller.setLocation);
rest.get('/wd/hub/session/:sessionId?/source', controller.getPageSource);
rest.get('/wd/hub/session/:sessionId?/alert_text', controller.getAlertText);
rest.post('/wd/hub/session/:sessionId?/alert_text', controller.setAlertText);
rest.post('/wd/hub/session/:sessionId?/accept_alert', controller.postAcceptAlert);
rest.post('/wd/hub/session/:sessionId?/dismiss_alert', controller.postDismissAlert);
rest.post('/wd/hub/session/:sessionId?/timeouts/implicit_wait', controller.implicitWait);
rest.post('/wd/hub/session/:sessionId?/timeouts/async_script', controller.asyncScriptTimeout);
rest.get('/wd/hub/session/:sessionId?/orientation', controller.getOrientation);
rest.post('/wd/hub/session/:sessionId?/orientation', controller.setOrientation);
rest.get('/wd/hub/session/:sessionId?/screenshot', controller.getScreenshot);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/element', controller.findElementFromElement);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/elements', controller.findElementsFromElement);
rest.post('/wd/hub/session/:sessionId?/touch/click', controller.doClick);
rest.post('/wd/hub/session/:sessionId?/touch/longclick', controller.touchLongClick);
rest.post('/wd/hub/session/:sessionId?/touch/down', controller.touchDown);
rest.post('/wd/hub/session/:sessionId?/touch/up', controller.touchUp);
rest.post('/wd/hub/session/:sessionId?/touch/move', controller.touchMove);
rest.post('/wd/hub/session/:sessionId?/touch/flick', controller.pickAFlickMethod);
rest.post('/wd/hub/session/:sessionId?/url', controller.postUrl);
rest.get('/wd/hub/session/:sessionId?/url', controller.getUrl);
rest.post('/wd/hub/session/:sessionId?/element/active', controller.active);
rest.get('/wd/hub/session/:sessionId?/window_handle', controller.getWindowHandle);
rest.get('/wd/hub/session/:sessionId?/window_handles', controller.getWindowHandles);
rest.post('/wd/hub/session/:sessionId?/window', controller.setWindow);
rest.delete('/wd/hub/session/:sessionId?/window', controller.closeWindow);
rest.get('/wd/hub/session/:sessionId?/window/:windowhandle?/size', controller.getWindowSize);
rest.post('/wd/hub/session/:sessionId?/execute', controller.execute);
rest.post('/wd/hub/session/:sessionId?/execute_async', controller.executeAsync);
rest.get('/wd/hub/session/:sessionId?/title', controller.title);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/submit', controller.submit);
rest.post('/wd/hub/session/:sessionId?/moveto', controller.moveTo);
rest.post('/wd/hub/session/:sessionId?/click', controller.clickCurrent);
rest.post('/wd/hub/session/:sessionId?/back', controller.back);
rest.post('/wd/hub/session/:sessionId?/forward', controller.forward);
rest.post('/wd/hub/session/:sessionId?/refresh', controller.refresh);
rest.get('/wd/hub/session/:sessionId?/cookie', controller.getCookies);
rest.post('/wd/hub/session/:sessionId?/cookie', controller.setCookie);
rest.delete('/wd/hub/session/:sessionId?/cookie', controller.deleteCookies);
rest.delete('/wd/hub/session/:sessionId?/cookie/:name', controller.deleteCookie);
rest.post('/wd/hub/session/:sessionId?/log', controller.getLog);
rest.get('/wd/hub/session/:sessionId?/log/types', controller.getLogTypes);
rest.post('/wd/hub/session/:sessionId?/timeouts', controller.timeouts);
rest.get('/wd/hub/session/:sessionId?/network_connection', controller.getNetworkConnection);
rest.post('/wd/hub/session/:sessionId?/network_connection', controller.setNetworkConnection);
// touch gesture endpoints
rest.post('/wd/hub/session/:sessionId?/touch/perform', controller.performTouch);
rest.post('/wd/hub/session/:sessionId?/touch/multi/perform', controller.performMultiAction);
// allow appium to receive async response
rest.post('/wd/hub/session/:sessionId?/receive_async_response', controller.receiveAsyncResponse);
//welcome page
rest.all('/welcome', controller.welcome);
// these are for testing purposes only
rest.post('/wd/hub/produce_error', controller.produceError);
rest.post('/wd/hub/crash', controller.crash);
rest.all('/test/guinea-pig', controller.guineaPig);
// IME specific
rest.get('/wd/hub/session/:sessionId?/ime/available_engines', controller.availableIMEEngines);
rest.get('/wd/hub/session/:sessionId?/ime/active_engine', controller.getActiveIMEEngine);
rest.get('/wd/hub/session/:sessionId?/ime/activated', controller.isIMEActivated);
rest.post('/wd/hub/session/:sessionId?/ime/deactivate', controller.deactivateIMEEngine);
rest.post('/wd/hub/session/:sessionId?/ime/activate', controller.activateIMEEngine);
// appium-specific extensions to JSONWP
rest.post('/wd/hub/session/:sessionId?/appium/device/shake', controller.mobileShake);
rest.post('/wd/hub/session/:sessionId?/appium/device/lock', controller.lock);
rest.post('/wd/hub/session/:sessionId?/appium/device/unlock', controller.unlock);
rest.post('/wd/hub/session/:sessionId?/appium/device/is_locked', controller.isLocked);
rest.post('/wd/hub/session/:sessionId?/appium/device/press_keycode', controller.pressKeyCode);
rest.post('/wd/hub/session/:sessionId?/appium/device/long_press_keycode', controller.longPressKeyCode);
rest.post('/wd/hub/session/:sessionId?/appium/device/keyevent', controller.keyevent);
rest.post('/wd/hub/session/:sessionId?/appium/device/rotate', controller.mobileRotation);
rest.get('/wd/hub/session/:sessionId?/appium/device/current_activity', controller.getCurrentActivity);
rest.post('/wd/hub/session/:sessionId?/appium/device/install_app', controller.installApp);
rest.post('/wd/hub/session/:sessionId?/appium/device/remove_app', controller.removeApp);
rest.post('/wd/hub/session/:sessionId?/appium/device/app_installed', controller.isAppInstalled);
rest.post('/wd/hub/session/:sessionId?/appium/device/hide_keyboard', controller.hideKeyboard);
rest.post('/wd/hub/session/:sessionId?/appium/device/push_file', controller.pushFile);
rest.post('/wd/hub/session/:sessionId?/appium/device/pull_file', controller.pullFile);
rest.post('/wd/hub/session/:sessionId?/appium/device/pull_folder', controller.pullFolder);
rest.post('/wd/hub/session/:sessionId?/appium/device/toggle_airplane_mode', controller.toggleFlightMode);
rest.post('/wd/hub/session/:sessionId?/appium/device/toggle_data', controller.toggleData);
rest.post('/wd/hub/session/:sessionId?/appium/device/toggle_wifi', controller.toggleWiFi);
rest.post('/wd/hub/session/:sessionId?/appium/device/toggle_location_services', controller.toggleLocationServices);
rest.post('/wd/hub/session/:sessionId?/appium/device/open_notifications', controller.openNotifications);
rest.post('/wd/hub/session/:sessionId?/appium/device/start_activity', controller.startActivity);
rest.post('/wd/hub/session/:sessionId?/appium/app/launch', controller.launchApp);
rest.post('/wd/hub/session/:sessionId?/appium/app/close', controller.closeApp);
rest.post('/wd/hub/session/:sessionId?/appium/app/reset', controller.reset);
rest.post('/wd/hub/session/:sessionId?/appium/app/background', controller.background);
rest.post('/wd/hub/session/:sessionId?/appium/app/end_test_coverage', controller.endCoverage);
rest.post('/wd/hub/session/:sessionId?/appium/app/strings', controller.getStrings);
rest.post('/wd/hub/session/:sessionId?/appium/element/:elementId?/value', controller.setValueImmediate);
rest.post('/wd/hub/session/:sessionId?/appium/element/:elementId?/replace_value', controller.replaceValue);
rest.post('/wd/hub/session/:sessionId?/appium/settings', controller.updateSettings);
rest.get('/wd/hub/session/:sessionId?/appium/settings', controller.getSettings);
// keep this at the very end!
rest.all('/*', controller.unknownCommand);
};
var routeNotYetImplemented = function (rest) {
rest.get('/wd/hub/session/:sessionId?/local_storage', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/local_storage', controller.notYetImplemented);
rest.delete('/wd/hub/session/:sessionId?/local_storage', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/local_storage/key/:key', controller.notYetImplemented);
rest.delete('/wd/hub/session/:sessionId?/local_storage/key/:key', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/local_storage/size', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/window/:windowhandle/size', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/window/:windowhandle/position', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/window/:windowhandle/position', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/window/:windowhandle/maximize', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/element/:elementId?', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/buttondown', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/buttonup', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/doubleclick', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/touch/scroll', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/touch/doubleclick', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/location', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/session_storage', controller.notYetImplemented);
rest.post('/wd/hub/session/:sessionId?/session_storage', controller.notYetImplemented);
rest.delete('/wd/hub/session/:sessionId?/session_storage', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/session_storage/key/:key', controller.notYetImplemented);
rest.delete('/wd/hub/session/:sessionId?/session_storage/key/:key', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/session_storage/size', controller.notYetImplemented);
rest.get('/wd/hub/session/:sessionId?/application_cache/status', controller.notYetImplemented);
};
总结
通过上面的分析,你是否知道了路由器模块的含义了?我个人是这么理解的,首先你想想路由器是干什么的。我们平时家里面都有一个路由器,最简单的理解是不是通过它能上网,我们上网无非就是查东西(get),然后上传资源(post),有的时候我们注册一个账号,想要修改一下账号(put),又或者把上传的资源删除了(delete)。这些都是要经过路由器的。那么我们的express服务器通过什么来连接呢,那自然是我们自己定义的routing路由器,一切的资源管理都是交给该路由器管理的。那它具体管理的啥,自然是你的一些请求发送过来后,应该做怎么样的处理,这个时候路由器早就给这些请求分配好了相应的回调函数,一旦对应的请求过来了,就会执行这些回调函数。
ok,今天的分析就到这了,明天继续。