Skip to content

Commit

Permalink
test: add test and doc for listen options (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and atian25 committed Jul 27, 2017
1 parent 3ef1de9 commit dda386e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
17 changes: 17 additions & 0 deletions docs/source/zh-cn/core/deployment.md
Expand Up @@ -59,6 +59,23 @@ EGG_SERVER_ENV=prod nohup node dispatch.js > stdout.log 2> stderr.log &
- 如果使用 Docker,可直接前台运行。
- 默认情况框架会创建和 CPU 核数相当的 app worker 数,可以充分的利用 CPU 资源。

### 启动配置项

你也可以在 `config.{env}.js` 中配置指定启动配置。

```js
// config/config.default.js
exports.cluster = {
listen: {
port: '7001',
hostname: '127.0.0.1',
// path: '/var/run/egg.sock',
}
}
```

`path``port``hostname` 均为 [server.listen](https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback) 的参数,`egg.startCluster` 方法传入的 port 优先级高于此配置。

### 自定义框架启动

如果应用使用了[自定义框架](../advanced/framework.md),还需要指定额外的参数,比如框架为 `yadan`
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/apps/app-server-with-hostname/app/router.js
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
this.body = 'done';
});
};
@@ -0,0 +1,10 @@
'use strict';

const address = require('address');

exports.keys = 'my keys';
exports.cluster = {
listen: {
hostname: address.ip(),
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/app-server-with-hostname/package.json
@@ -0,0 +1,3 @@
{
"name": "app-server-with-hostname"
}
33 changes: 32 additions & 1 deletion test/lib/cluster/app_worker.test.js
@@ -1,12 +1,14 @@
'use strict';

const request = require('supertest');
const address = require('address');
const assert = require('assert');
const utils = require('../../utils');

describe('test/lib/cluster/app_worker.test.js', () => {
let app;
before(() => {
app = utils.cluster('apps/app-server');
app.coverage(true);
return app.ready();
});
after(() => app.close());
Expand All @@ -16,4 +18,33 @@ describe('test/lib/cluster/app_worker.test.js', () => {
.get('/')
.expect('true');
});

describe('listen hostname', () => {
let app;
before(() => {
app = utils.cluster('apps/app-server-with-hostname');
return app.ready();
});
after(() => app.close());

it('should refuse other ip', function* () {
const url = address.ip() + ':' + app.port;

yield request(url)
.get('/')
.expect('done')
.expect(200);

try {
yield request('http://127.0.0.1:17010')
.get('/')
.expect('done')
.expect(200);
throw new Error('should not run');
} catch (err) {
assert(err.message === 'ECONNREFUSED: Connection refused');
}
});
});

});

0 comments on commit dda386e

Please sign in to comment.