1. 路由的原理:
当服务端接收到HTTP请求时,可以通过onRequest() 获取到url, pathname,query,及paramParams参数
为了解析这些数据需要使用url和querystring模块
当在服务端的onRequest()方法中解析出pathname后,将pathname传入路由模块,路由模块将进行分发处理;即需要在解析出pathname后及创建Response对象之前将pathname传给路由模块进行分发处理
伪代码是这样的:
# 请求处理函数 function onRequest(url){ 解析url得到pathname pathname = querystring.parse(url) #调用路由函数 route(pathname) 返回 createResponse() } # 启动服务器等待客户端请求 server.start(onRequest())