Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING CHANGE] feat: remove notfound.enableRedirect #368

Merged
merged 2 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 2 additions & 7 deletions app/middleware/notfound.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ module.exports = options => {
return;
}

if (options.enableRedirect && options.pageUrl) {
if (options.pageUrl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还要防止死循环,确保 referer 不是自己,免得开发者配置了 options.pageUrl ='/404',然后又没有实现 404 页面

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

比较难,例如 referrer 为 http://dev.xxx.com/404,没法判断它是自己,这个还是交由开发者自己保证吧,开发时会发现的。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果 pageUrl 是当前站点的,以 / 开头,可以通过判断 this.path 是否等于 pageUrl 来解决的

this.realStatus = 404;
this.redirect(options.pageUrl);
return;
}
const title = '<h1>404 Not Found</h1>';
if (!options.enableRedirect && options.pageUrl) {
this.body = `${title}Because you are in a non-prod environment, you will be looking at this page, otherwise it will jump to ${options.pageUrl}`;
} else {
this.body = title;
}
this.body = '<h1>404 Not Found</h1>';
};
};
1 change: 0 additions & 1 deletion config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ module.exports = appInfo => {
*/
exports.notfound = {
pageUrl: '',
enableRedirect: appInfo.env === 'prod',
};

/**
Expand Down
65 changes: 59 additions & 6 deletions docs/source/zh-cn/core/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ this.runInBackground(function* () {

| 请求需求的格式 | 环境 | errorPageUrl 是否配置 | 返回内容 |
|-------------|------|----------------------|--------|
| html & text | local & unittest | - | onerror 自带的错误页面,展示详细的错误信息 |
| html & text | 其他 | 是 | 重定向到 errorPageUrl |
| html & text | 其他 | 否 | onerror 自带的没有错误信息的简单错误页(不推荐) |
| json | local & unittest | - | json 对象,带详细的错误信息 |
| json | 其他 | - | json 对象,不带详细的错误信息 |
| HTML & TEXT | local & unittest | - | onerror 自带的错误页面,展示详细的错误信息 |
| HTML & TEXT | 其他 | 是 | 重定向到 errorPageUrl |
| HTML & TEXT | 其他 | 否 | onerror 自带的没有错误信息的简单错误页(不推荐) |
| JSON | local & unittest | - | JSON 对象,带详细的错误信息 |
| JSON | 其他 | - | json 对象,不带详细的错误信息 |

### errorPageUrl

onerror 插件的配置中支持 errorPageUrl 属性,当配置了 errorPageUrl 时,一旦用户请求线上应用的 html 页面异常,就会重定向到这个地址。
onerror 插件的配置中支持 errorPageUrl 属性,当配置了 errorPageUrl 时,一旦用户请求线上应用的 HTML 页面异常,就会重定向到这个地址。

在 `config/config.default.js` 中

Expand Down Expand Up @@ -111,3 +111,56 @@ module.exports = {
},
};
```

## 404

框架并不会将服务端返回的 404 状态当做异常来处理,但是框架提供了当响应为 404 且没有返回 body 时的默认响应。

- 当请求被框架判定为需要 JSON 格式的响应时,会返回一段 JSON:

```json
{ "message": "Not Found" }
```

- 当请求被框架判定为需要 HTML 格式的响应时,会返回一段 HTML:

```html
<h1>404 Not Found</h1>
```

框架支持通过配置,将默认的 HTML 请求的 404 响应重定向到指定的页面。

```js
// config/config.default.js
module.exports = {
notfound: {
pageUrl: '/404.html',
},
};
```

### 自定义 404 响应

在一些场景下,我们需要自定义服务器 404 时的响应,和自定义异常处理一样,我们也只需要加入一个中间件即可对 404 做统一处理:

```js
// app/middleware/notfound_handler.js
module.exports = () => {
return function* (next) {
yield next;
if (this.status === 404 && !this.body) {
if (this.acceptJSON) this.body = { error: 'Not Found' };
else this.body = '<h1>Page Not Found</h1>';
}
};
};
```

在配置中引入中间件:

```js
// config/config.default.js
module.exports = {
middleware: [ 'notfoundHander' ],
};
```
11 changes: 1 addition & 10 deletions test/app/middleware/notfound.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ describe('test/app/middleware/notfound.test.js', () => {

afterEach(mm.restore);

it('should 302 redirect to 404.html on production env', () => {
mm(app.config.notfound, 'enableRedirect', true);
it('should 302 redirect to 404.html', () => {
return request(app.callback())
.get('/test/404')
.set('Accept', 'test/html')
.expect('Location', 'https://eggjs.org/404')
.expect(302);
});

it('should show 404 on dev env', () => {
return request(app.callback())
.get('/test/404')
.expect('<h1>404 Not Found</h1>Because you are in a non-prod environment, you will be looking at this page, otherwise it will jump to https://eggjs.org/404')
.expect(404);
});

it('should 404 json response', () => {
return request(app.callback())
.get('/test/404.json?ctoken=404')
Expand Down Expand Up @@ -71,7 +63,6 @@ describe('test/app/middleware/notfound.test.js', () => {

it('should 302 redirect to custom /404 on production env', done => {
done = pedding(2, done);
mm(app.config.notfound, 'enableRedirect', true);

request(app.callback())
.get('/test/404')
Expand Down