diff --git a/chapter3/entry.js b/chapter3/entry.js index 2c28254..b025ff8 100644 --- a/chapter3/entry.js +++ b/chapter3/entry.js @@ -1,8 +1,33 @@ const mount = require('koa-mount'); const koa = require('koa'); +const url = require('url') const app = new koa; +/** + * 由于很多同学打开demo的时候习惯性没加最后的/ + * 导致页面在访问css或者js内容的时候,会因为拼错链接而返回404 + * 所以这里加上一个判断,没有以/结尾的时候要进行重定向。 + * + * 当然,在koa-mount进行路由匹配的时候直接匹配 /download/ 也是可以避免出现错误页面的 + * 但是为了用户考虑,最好就是帮助用户修正错误。 + */ +app.use(async (ctx, next)=> { + const parsedUrl = url.parse(ctx.url); + if ( + (parsedUrl.pathname === '/download') || + (parsedUrl.pathname === '/detail') || + (parsedUrl.pathname === '/play') || + (parsedUrl.pathname === '/list') + ) { + parsedUrl.pathname = parsedUrl.pathname + '/' + ctx.redirect(url.format(parsedUrl)); + return + } + + await next(); +}) + app.use( mount('/download', require('./1.download/index')) ) diff --git a/chapter3/readme.md b/chapter3/readme.md index bc8254a..a98dd39 100644 --- a/chapter3/readme.md +++ b/chapter3/readme.md @@ -1,3 +1,7 @@ # 先跑npm run microservice # 再跑npm run bff -# 访问localhost:3000/download/ \ No newline at end of file +# 访问demo页 +1. http://localhost:3000/download/ +2. http://localhost:3000/detail/?columnid=233 +3. http://localhost:3000/play/ +4. http://localhost:3000/list/ \ No newline at end of file diff --git a/chapter5/business/play/data.js b/chapter5/business/play/data.js index ff5cf7a..e2f521a 100644 --- a/chapter5/business/play/data.js +++ b/chapter5/business/play/data.js @@ -81,15 +81,15 @@ /******/ /******/ /******/ // Load entry module and return exports -/******/ return __webpack_require__(__webpack_require__.s = "./workspace/src/page.data.js"); +/******/ return __webpack_require__(__webpack_require__.s = "./src/page.data.js"); /******/ }) /************************************************************************/ /******/ ({ -/***/ "./workspace/src/page.data.js": -/*!************************************!*\ - !*** ./workspace/src/page.data.js ***! - \************************************/ +/***/ "./src/page.data.js": +/*!**************************!*\ + !*** ./src/page.data.js ***! + \**************************/ /*! no static exports found */ /***/ (function(module, exports, __webpack_require__) { @@ -101,7 +101,7 @@ module.exports = { port: 4000, - protobufFile: __webpack_require__(/*! ./workspace/src/proto/detail.proto */ "./workspace/src/proto/detail.proto"), + protobufFile: __webpack_require__(/*! ./src/proto/detail.proto */ "./src/proto/detail.proto"), requestStruct: 'ColumnRequest', responseStruct: 'ColumnResponse', @@ -120,7 +120,7 @@ module.exports = { }, then: function (res) { - return JSON.parse(res).data//.list; + return JSON.parse(res).data.list; }, catch: function () { @@ -131,10 +131,10 @@ module.exports = { /***/ }), -/***/ "./workspace/src/proto/detail.proto": -/*!******************************************!*\ - !*** ./workspace/src/proto/detail.proto ***! - \******************************************/ +/***/ "./src/proto/detail.proto": +/*!********************************!*\ + !*** ./src/proto/detail.proto ***! + \********************************/ /*! no static exports found */ /***/ (function(module, exports) { diff --git a/chapter5/readme.md b/chapter5/readme.md index a520fb6..a4dc3c8 100644 --- a/chapter5/readme.md +++ b/chapter5/readme.md @@ -2,4 +2,17 @@ * backend 后端服务内容 * business 业务配置中心,模拟云函数架构存储函数内容的地方 * server 服务端运行逻辑 - * workspace 模拟开发者工作空间 \ No newline at end of file + * workspace 模拟开发者工作空间 + +* 运行方式 +先打开终端运行 +``` +node server/test.js +``` + +再打开另一个终端运行 +``` +node backend/run.js +``` + +然后打开浏览器访问`http://localhost:3000/play?columnid=1` \ No newline at end of file diff --git a/chapter5/server/run.js b/chapter5/server/run.js index 2452648..0caebed 100644 --- a/chapter5/server/run.js +++ b/chapter5/server/run.js @@ -1,5 +1,5 @@ const mount = require('koa-mount') - +const requestFactory = require('./request-factory') const createTemplate = require('./create-template'); requestFactory.registerProtocol('geek-rpc', @@ -33,19 +33,23 @@ module.exports = function (app) { koa.use( mount(routepath, async (ctx) => { ctx.status = 200; - const result = {}; await Promise.all( Object.keys(requests).map(key => { return requests[key](ctx.query) .then(res => { - result[key] = res; - return res; + result[key] = res.result; + return res.result; }) }) ) - ctx.body = template(result); + try { + ctx.body = template(result); + } catch(e) { + ctx.status = 500; + ctx.body = e.stack; + } }) ); }); diff --git a/chapter5/workspace/src/page.data.js b/chapter5/workspace/src/page.data.js index 27c7af9..424611a 100644 --- a/chapter5/workspace/src/page.data.js +++ b/chapter5/workspace/src/page.data.js @@ -25,7 +25,7 @@ module.exports = { }, then: function (res) { - return JSON.parse(res).data//.list; + return JSON.parse(res).data.list; }, catch: function () { diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..65724b2 --- /dev/null +++ b/readme.md @@ -0,0 +1,6 @@ +# 浏览指引 + +1. 第二章里,每一个小文件夹都是一个demo +2. 第三章里,整个第三章是一个完整项目,请观看第三章的readme来确定启动方式 +3. 第四章里,每一个小文件夹都是一个demo +4. 第五章里,整个第五章是一个完整项目,请观看第五章的readme来确定启动方式 \ No newline at end of file