Skip to content

Commit 05f7fa1

Browse files
committed
feat: 移除exec脚本执行器,修改bundle脚本逻辑
1 parent c014fd4 commit 05f7fa1

File tree

3 files changed

+108
-179
lines changed

3 files changed

+108
-179
lines changed

docs/server-end/framework/egg/tutorial/控制器.md

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ const Controller = require('egg').Controller
3838

3939
class PostController extends Controller {
4040
async create() {
41-
const {ctx, service} = this
41+
const { ctx, service } = this
4242
const createRule = {
43-
title: {type: 'string'},
44-
content: {type: 'string'},
43+
title: { type: 'string' },
44+
content: { type: 'string' },
4545
}
4646
// 校验参数
4747
ctx.validate(createRule)
4848
// 组装参数
4949
const author = ctx.session.userId
50-
const req = Object.assign(ctx.request.body, {author})
50+
const req = Object.assign(ctx.request.body, { author })
5151
// 调用 Service 进行业务处理
5252
const res = await service.post.create(req)
5353
// 设置响应内容和响应状态码
54-
ctx.body = {id: res.id}
54+
ctx.body = { id: res.id }
5555
ctx.status = 201
5656
}
5757
}
@@ -65,7 +65,7 @@ module.exports = PostController
6565
```js
6666
// app/router.js
6767
module.exports = (app) => {
68-
const {router, controller} = app
68+
const { router, controller } = app
6969
router.post('createPost', '/api/posts', controller.post.create)
7070
}
7171
```
@@ -99,7 +99,7 @@ Controller 基类的方式封装应用中常用的方法。
9999

100100
```js
101101
// app/core/base_controller.js
102-
const {Controller} = require('egg')
102+
const { Controller } = require('egg')
103103

104104
class BaseController extends Controller {
105105
get user() {
@@ -144,18 +144,18 @@ class PostController extends Controller {
144144
// app/controller/post.js
145145
exports.create = async (ctx) => {
146146
const createRule = {
147-
title: {type: 'string'},
148-
content: {type: 'string'},
147+
title: { type: 'string' },
148+
content: { type: 'string' },
149149
}
150150
// 校验参数
151151
ctx.validate(createRule)
152152
// 组装参数
153153
const author = ctx.session.userId
154-
const req = Object.assign(ctx.request.body, {author})
154+
const req = Object.assign(ctx.request.body, { author })
155155
// 调用 service 进行业务处理
156156
const res = await ctx.service.post.create(req)
157157
// 设置响应内容和响应状态码
158-
ctx.body = {id: res.id}
158+
ctx.body = { id: res.id }
159159
ctx.status = 201
160160
}
161161
```
@@ -208,8 +208,7 @@ if (key.startsWith('egg')) {
208208

209209
### Queries
210210

211-
有时候系统会设计成让用户传递相同的 `key`,例如 `GET /posts?category=egg&id=1&id=2&id=3`。针对此类情况,\*
212-
\*框架提供了 `ctx.queries` 对象,这个对象也解析了 `Query String`,但是它不会丢弃任何一个重复的数据,而是将他们都放到一个数组中
211+
有时候系统会设计成让用户传递相同的 `key`,例如 `GET /posts?category=egg&id=1&id=2&id=3`。针对此类情况,\* \*框架提供了 `ctx.queries` 对象,这个对象也解析了 `Query String`,但是它不会丢弃任何一个重复的数据,而是将他们都放到一个数组中
213212
\*\*
214213

215214
```js
@@ -328,10 +327,9 @@ exports.multipart = {
328327
前端上传代码示例:
329328

330329
```html
331-
332330
<form method="POST" action="/upload?_csrf={{ ctx.csrf | safe }}" enctype="multipart/form-data">
333-
title: <input name="title"/> file: <input name="file" type="file"/>
334-
<button type="submit">Upload</button>
331+
title: <input name="title" /> file: <input name="file" type="file" />
332+
<button type="submit">Upload</button>
335333
</form>
336334
```
337335

@@ -344,14 +342,15 @@ const fs = require('mz/fs')
344342

345343
module.exports = class extends Controller {
346344
async upload() {
347-
const {ctx} = this
345+
const { ctx } = this
348346
const file = ctx.request.files[0]
349347
const name = `egg-multipart-test/${path.basename(file.filename)}`
350348
let result
351349
try {
352350
// 处理文件,比如上传到云端
353351
result = await ctx.oss.put(name, file.filepath)
354-
} finally {
352+
}
353+
finally {
355354
// 需要删除临时文件
356355
await fs.unlink(file.filepath)
357356
}
@@ -366,17 +365,15 @@ module.exports = class extends Controller {
366365
```
367366

368367
上面的是基于最简单的单个文件上传进行的代码示例,借助`ctx.request.files`
369-
数组,取0号元素完成的。而在实际的应用中,多个文件的上传的场景是非常常见的,此时也是需要借助`ctx.request.files`数组,\*
370-
\*不过需要多注意一步——数组遍历\*\*
368+
数组,取0号元素完成的。而在实际的应用中,多个文件的上传的场景是非常常见的,此时也是需要借助`ctx.request.files`数组,\* \*不过需要多注意一步——数组遍历\*\*
371369

372370
前端多文件上传:
373371

374372
```html
375-
376373
<form method="POST" action="/upload?_csrf={{ ctx.csrf | safe }}" enctype="multipart/form-data">
377-
title: <input name="title"/> file1: <input name="file1" type="file"/> file2:
378-
<input name="file2" type="file"/>
379-
<button type="submit">Upload</button>
374+
title: <input name="title" /> file1: <input name="file1" type="file" /> file2:
375+
<input name="file2" type="file" />
376+
<button type="submit">Upload</button>
380377
</form>
381378
```
382379

@@ -389,7 +386,7 @@ const fs = require('mz/fs')
389386

390387
module.exports = class extends Controller {
391388
async upload() {
392-
const {ctx} = this
389+
const { ctx } = this
393390
console.log(ctx.request.body)
394391
console.log('got %d files', ctx.request.files.length)
395392
// 遍历文件
@@ -403,7 +400,8 @@ module.exports = class extends Controller {
403400
try {
404401
// 处理文件,比如上传到云端
405402
result = await ctx.oss.put(`egg-multipart-test/${file.filename}`, file.filepath)
406-
} finally {
403+
}
404+
finally {
407405
// 需要删除临时文件
408406
await fs.unlink(file.filepath)
409407
}
@@ -423,10 +421,9 @@ module.exports = class extends Controller {
423421
前端示例:
424422

425423
```html
426-
427424
<form method="POST" action="/upload?_csrf={{ ctx.csrf | safe }}" enctype="multipart/form-data">
428-
title: <input name="title"/> file: <input name="file" type="file"/>
429-
<button type="submit">Upload</button>
425+
title: <input name="title" /> file: <input name="file" type="file" />
426+
<button type="submit">Upload</button>
430427
</form>
431428
```
432429

@@ -446,7 +443,8 @@ class UploaderController extends Controller {
446443
let result
447444
try {
448445
result = await ctx.oss.put(name, stream)
449-
} catch (err) {
446+
}
447+
catch (err) {
450448
// 必须将上传的文件流消费掉,要不然浏览器响应会卡死
451449
await sendToWormhole(stream)
452450
throw err
@@ -489,7 +487,8 @@ class UploaderController extends Controller {
489487
console.log(`value: ${part[1]}`)
490488
console.log(`valueTruncated: ${part[2]}`)
491489
console.log(`fieldnameTruncated: ${part[3]}`)
492-
} else {
490+
}
491+
else {
493492
if (!part.filename) {
494493
// 这时是用户没有选择文件就点击了上传(part 是 file stream,但是 part.filename 为空)
495494
// 需要做出处理,例如给出错误提示消息
@@ -504,7 +503,8 @@ class UploaderController extends Controller {
504503
let result
505504
try {
506505
result = await ctx.oss.put(`egg-multipart-test/${part.filename}`, part)
507-
} catch (err) {
506+
}
507+
catch (err) {
508508
// 必须将上传的文件流消费掉,要不然浏览器响应会卡死
509509
await sendToWormhole(part)
510510
throw err
@@ -522,29 +522,29 @@ module.exports = UploaderController
522522
**为了保证文件上传的安全,框架限制了支持的的文件格式,框架默认支持白名单**
523523

524524
```js
525-
// images
525+
// images
526526
'.jpg', '.jpeg', // image/jpeg
527-
'.png', // image/png, image/x-png
528-
'.gif', // image/gif
529-
'.bmp', // image/bmp
530-
'.wbmp', // image/vnd.wap.wbmp
531-
'.webp',
532-
'.tif',
533-
'.psd',
527+
'.png', // image/png, image/x-png
528+
'.gif', // image/gif
529+
'.bmp', // image/bmp
530+
'.wbmp', // image/vnd.wap.wbmp
531+
'.webp',
532+
'.tif',
533+
'.psd',
534534
// text
535-
'.svg',
536-
'.js', '.jsx',
537-
'.json',
538-
'.css', '.less',
539-
'.html', '.htm',
540-
'.xml',
535+
'.svg',
536+
'.js', '.jsx',
537+
'.json',
538+
'.css', '.less',
539+
'.html', '.htm',
540+
'.xml',
541541
// tar
542-
'.zip',
543-
'.gz', '.tgz', '.gzip',
542+
'.zip',
543+
'.gz', '.tgz', '.gzip',
544544
// video
545-
'.mp3',
546-
'.mp4',
547-
'.avi'
545+
'.mp3',
546+
'.mp4',
547+
'.avi'
548548
```
549549

550550
用户可以通过在 `config/config.default.js` 中配置**来新增支持的文件扩展名,或者重写整个白名单**
@@ -600,8 +600,7 @@ module.exports = {
600600

601601
通过这个 `Getter` 获取 `protocol` 时,首先会判断当前连接是否是加密连接,如果是加密连接,返回 `https`
602602

603-
如果处于非加密连接时,优先读通过 `config.protocolHeaders` 中配置的 header 的值来判断是 HTTP 还是 `https`\*
604-
\*如果读取不到,可以在配置中通过 config.protocol 来设置兜底值,默认为 HTTP。\*\*
603+
如果处于非加密连接时,优先读通过 `config.protocolHeaders` 中配置的 header 的值来判断是 HTTP 还是 `https`\* \*如果读取不到,可以在配置中通过 config.protocol 来设置兜底值,默认为 HTTP。\*\*
605604

606605
`config.protocolHeaders` 默认配置为 `x-forwarded-proto`
607606

@@ -631,7 +630,7 @@ HTTP 请求都是无状态的,但是 `Web` 应用通常都需要知道发起
631630
class CookieController extends Controller {
632631
// 添加cookies
633632
async add() {
634-
const {ctx} = this
633+
const { ctx } = this
635634
let count = ctx.cookies.get('count')
636635
count = count ? Number(count) : 0
637636
ctx.cookies.set('count', ++count)
@@ -640,7 +639,7 @@ class CookieController extends Controller {
640639

641640
// 删除cookies
642641
async remove() {
643-
const {ctx} = this
642+
const { ctx } = this
644643
const count = ctx.cookies.set('count', null)
645644
ctx.status = 204
646645
}
@@ -660,7 +659,7 @@ class CookieController extends Controller {
660659
```js
661660
class PostController extends Controller {
662661
async fetchPosts() {
663-
const {ctx} = this
662+
const { ctx } = this
664663
// 获取 Session 上的内容
665664
const userId = ctx.session.userId
666665
const posts = await ctx.service.post.fetch(userId)
@@ -704,8 +703,8 @@ class PostController extends Controller {
704703
// 校验参数
705704
// 如果不传第二个参数会自动校验 `ctx.request.body`
706705
this.ctx.validate({
707-
title: {type: 'string'},
708-
content: {type: 'string'},
706+
title: { type: 'string' },
707+
content: { type: 'string' },
709708
})
710709
}
711710
}
@@ -720,9 +719,10 @@ class PostController extends Controller {
720719
const ctx = this.ctx
721720
try {
722721
ctx.validate(createRule)
723-
} catch (err) {
722+
}
723+
catch (err) {
724724
ctx.logger.warn(err.errors)
725-
ctx.body = {success: false}
725+
ctx.body = { success: false }
726726
}
727727
}
728728
};
@@ -737,7 +737,8 @@ class PostController extends Controller {
737737
app.validator.addRule('json', (rule, value) => {
738738
try {
739739
JSON.parse(value)
740-
} catch (err) {
740+
}
741+
catch (err) {
741742
return 'must be json string'
742743
}
743744
})
@@ -748,9 +749,9 @@ app.validator.addRule('json', (rule, value) => {
748749
```js
749750
class PostController extends Controller {
750751
async handler() {
751-
const {ctx} = this
752+
const { ctx } = this
752753
// query.test 字段必须是 json 字符串
753-
const rule = {test: 'json'}
754+
const rule = { test: 'json' }
754755
ctx.validate(rule, ctx.query)
755756
}
756757
};
@@ -767,13 +768,13 @@ class PostController extends Controller {
767768
```js
768769
class PostController extends Controller {
769770
async create() {
770-
const {ctx} = this
771+
const { ctx } = this
771772
const author = ctx.session.userId
772773
// Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象
773-
const req = Object.assign(ctx.request.body, {author})
774+
const req = Object.assign(ctx.request.body, { author })
774775
// 调用 service 进行业务处理
775776
const res = await ctx.service.post.create(req)
776-
ctx.body = {id: res.id}
777+
ctx.body = { id: res.id }
777778
ctx.status = 201
778779
}
779780
}
@@ -811,7 +812,7 @@ class PostController extends Controller {
811812
```js
812813
class ViewController extends Controller {
813814
async show() {
814-
const {ctx} = this
815+
const { ctx } = this
815816
ctx.body = {
816817
name: 'egg',
817818
category: 'framework',
@@ -832,7 +833,7 @@ body 设置成一个 Stream,并会同时处理好这个 Stream 上的错误事
832833
```js
833834
class ProxyController extends Controller {
834835
async proxy() {
835-
const {ctx} = this
836+
const { ctx } = this
836837
const result = await ctx.curl(url, {
837838
streaming: true,
838839
})
@@ -852,7 +853,7 @@ class ProxyController extends Controller {
852853
class HomeController extends Controller {
853854
async index() {
854855
const ctx = this.ctx
855-
await ctx.render('home.tpl', {name: 'egg'})
856+
await ctx.render('home.tpl', { name: 'egg' })
856857
// ctx.body = await ctx.renderString('hi, {{ name }}', { name: 'egg' });
857858
}
858859
};
@@ -916,9 +917,9 @@ exports.jsonp = {
916917
```js
917918
// app/router.js
918919
module.exports = (app) => {
919-
const {router, controller, jsonp} = app
920-
router.get('/api/posts/:id', jsonp({callback: 'callback'}), controller.posts.show)
921-
router.get('/api/posts', jsonp({callback: 'cb'}), controller.posts.list)
920+
const { router, controller, jsonp } = app
921+
router.get('/api/posts/:id', jsonp({ callback: 'callback' }), controller.posts.show)
922+
router.get('/api/posts', jsonp({ callback: 'cb' }), controller.posts.list)
922923
}
923924
```
924925

0 commit comments

Comments
 (0)