Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions chapter3/entry.js
Original file line number Diff line number Diff line change
@@ -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'))
)
Expand Down
6 changes: 5 additions & 1 deletion chapter3/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 先跑npm run microservice
# 再跑npm run bff
# 访问localhost:3000/download/
# 访问demo页
1. http://localhost:3000/download/
2. http://localhost:3000/detail/?columnid=233
3. http://localhost:3000/play/
4. http://localhost:3000/list/
22 changes: 11 additions & 11 deletions chapter5/business/play/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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__) {

Expand All @@ -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',
Expand All @@ -120,7 +120,7 @@ module.exports = {
},

then: function (res) {
return JSON.parse(res).data//.list;
return JSON.parse(res).data.list;
},

catch: function () {
Expand All @@ -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) {

Expand Down
15 changes: 14 additions & 1 deletion chapter5/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@
* backend 后端服务内容
* business 业务配置中心,模拟云函数架构存储函数内容的地方
* server 服务端运行逻辑
* workspace 模拟开发者工作空间
* workspace 模拟开发者工作空间

* 运行方式
先打开终端运行
```
node server/test.js
```

再打开另一个终端运行
```
node backend/run.js
```

然后打开浏览器访问`http://localhost:3000/play?columnid=1`
14 changes: 9 additions & 5 deletions chapter5/server/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mount = require('koa-mount')

const requestFactory = require('./request-factory')
const createTemplate = require('./create-template');

requestFactory.registerProtocol('geek-rpc',
Expand Down Expand Up @@ -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;
}
})
);
});
Expand Down
2 changes: 1 addition & 1 deletion chapter5/workspace/src/page.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
},

then: function (res) {
return JSON.parse(res).data//.list;
return JSON.parse(res).data.list;
},

catch: function () {
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 浏览指引

1. 第二章里,每一个小文件夹都是一个demo
2. 第三章里,整个第三章是一个完整项目,请观看第三章的readme来确定启动方式
3. 第四章里,每一个小文件夹都是一个demo
4. 第五章里,整个第五章是一个完整项目,请观看第五章的readme来确定启动方式