Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8d221d0

Browse files
ntvsx193thetutlage
authored andcommitted
feat(server): added the ability to obtain instance http.createServer () for socket.io (#165)
* feat(server): added the ability to obtain instance http.createServer () for socket.io * fix(server): stupid semicolon * fix(server): stupid "Return statement should not contain assignment." * fix(server): stupid name variables & methods, add unit test on check httpInstance
1 parent 6df2868 commit 8d221d0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Server/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Server {
3434
this.config = Config
3535
this.event = Event
3636
this.log = new CatLog('adonis:framework')
37+
this.httpInstance = null
3738
}
3839

3940
/**
@@ -264,6 +265,19 @@ class Server {
264265
})
265266
}
266267

268+
/**
269+
*
270+
* @returns {*}
271+
* @public
272+
*/
273+
getInstance () {
274+
if (!this.httpInstance) {
275+
this.httpInstance = http.createServer(this.handle.bind(this))
276+
}
277+
278+
return this.httpInstance
279+
}
280+
267281
/**
268282
* starting a server on a given port and host
269283
*
@@ -277,7 +291,7 @@ class Server {
277291
*/
278292
listen (host, port) {
279293
this.log.info('serving app on %s:%s', host, port)
280-
http.createServer(this.handle.bind(this)).listen(port, host)
294+
this.getInstance().listen(port, host)
281295
}
282296

283297
}

test/unit/server.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,16 @@ describe("Server", function () {
278278
expect(res.error.text).to.equal('Forbidden')
279279
})
280280

281+
it("should server instance is null", function * () {
282+
expect(this.server.httpInstance).to.be.null
283+
})
284+
285+
it("should server instance is http.Server", function * () {
286+
const httpServer = this.server.getInstance()
287+
expect(httpServer).to.be.instanceOf(http.Server)
288+
expect(this.server.httpInstance).to.be.instanceOf(http.Server)
289+
})
290+
281291
it('should listen to server on a given port and host using listen method', function * () {
282292
Route.get('/','HomeController.index')
283293
this.server.listen('0.0.0.0', 8000)

0 commit comments

Comments
 (0)