Skip to content

Commit

Permalink
BaseApp.build
Browse files Browse the repository at this point in the history
  • Loading branch information
demingongo committed Sep 16, 2021
1 parent 6f3041b commit d94d058
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ server.listen(8000, '0.0.0.0', () => {
console.log(app.meta)
});
```

```js
const https = require('https');
const httpsServerOptions = {};

const server = app.build(httpsServerOptions, https);
server.listen(8000);
```
## References

- [Express](https://expressjs.com)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@novice1/app",
"private": false,
"version": "0.2.0",
"version": "0.2.1",
"description": "Create Express applications.",
"main": "lib/index.js",
"homepage": "https://novice1.000webhostapp.com/app/",
Expand Down
30 changes: 27 additions & 3 deletions src/baseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export interface IApp {
get server(): http.Server | undefined;
addRouters(routers: IRouter[] | IRouter): IApp;
addOptions(options: Options): IApp;
build(options?: http.ServerOptions): http.Server;
build<T extends http.ServerOptions = http.ServerOptions>(options?: T | null, mod?: {
createServer(requestListener?: http.RequestListener): http.Server;
createServer(options: T, requestListener?: http.RequestListener | undefined): http.Server;
}): http.Server;
disable(setting: string): IApp;
enable(setting: string): IApp;
disabled(setting: string): boolean;
Expand Down Expand Up @@ -196,7 +199,20 @@ export abstract class BaseApp implements IApp {
return this;
}

build(options?: http.ServerOptions): http.Server {
/**
*
* @param options
* @param mod Module that will create the server (e.g.: require('https'))
* @example ```js
* const https = require('https');
* const server = app.build(HTTPS_SERVER_OPTIONS, https);
* server.listen(PORT);
* ```
*/
build<T extends http.ServerOptions = http.ServerOptions>(options?: T | null, mod?: {
createServer(requestListener?: http.RequestListener): http.Server;
createServer(options: T, requestListener?: http.RequestListener | undefined): http.Server;
}): http.Server {
if (!this.#server /*&& !this.#isBuilding*/) {
this.#isBuilding = true;

Expand All @@ -207,7 +223,15 @@ export abstract class BaseApp implements IApp {
this.#isBuilt = true;
this.#isBuilding = false;

this.#server = http.createServer(options || {}, this.__app);
if (mod) {
if (options) {
this.#server = mod.createServer(options, this.__app);
} else {
this.#server = mod.createServer(this.__app);
}
} else {
this.#server = http.createServer(options || {}, this.__app);
}
}

return this.#server;
Expand Down
7 changes: 3 additions & 4 deletions test/src/01_test.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import routing from '@novice1/routing';
import { Server } from 'http';
import http, { Server } from 'http';
import { FrameworkApp } from '../../src/index';
import { expect } from 'chai';



describe('Starting server', function() {

const { logger: Log } = this.ctx.kaukau;
Expand Down Expand Up @@ -56,7 +54,8 @@ describe('Starting server', function() {
it('shoud start server on localhost:8080 (app.listen)', function(done) {
const port = 8080;
const ip = '0.0.0.0';
server = app.listen(port, ip, () => {
server = app.build(null, http);
/*server = */app.listen(port, ip, () => {
Log.info(`Application worker ${process.pid} started...`);
console.log(app.meta)
expect(app.meta)
Expand Down

0 comments on commit d94d058

Please sign in to comment.