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

Commit 30cfe41

Browse files
committed
refactor(server): Increased static server priority over route handler
Now static resources are checked first and if 404 is thrown, than only request is routed to the routes handler
1 parent ed3d3dc commit 30cfe41

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/Server/index.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,22 @@ class Server {
3434
* @private
3535
*/
3636
_finalHandler (resolvedRoute, request, response) {
37-
/**
38-
* if route is not registered, try looking for a static resource
39-
* or simply throw an error if static resource is not found
40-
*/
41-
if (!resolvedRoute.handler) {
42-
this.static.serve(request.request, request.response)
43-
.catch(function (e) {
44-
helpers.handleRequestError(e, request, response)
45-
})
46-
return
47-
}
48-
4937
/**
5038
* calling request action handler by making a middleware
5139
* layer of named middleware and finally invoking
5240
* route action.
5341
*/
54-
helpers.callRouteAction(resolvedRoute, request, response, this.middleware, this.helpers.appNameSpace())
42+
if (resolvedRoute.handler) {
43+
helpers.callRouteAction(resolvedRoute, request, response, this.middleware, this.helpers.appNameSpace())
44+
return
45+
}
46+
const error = new Error(`Route not found ${request.url()}`)
47+
error.status = 404
48+
throw error
49+
}
50+
51+
_staticHandler (request, response) {
52+
return this.static.serve(request.request, request.response)
5553
}
5654

5755
/**
@@ -69,6 +67,7 @@ class Server {
6967
const session = new this.Session(req, res)
7068
const requestUrl = request.url()
7169
request.session = session
70+
this.log.verbose('request on url %s ', req.url)
7271

7372
/**
7473
* making request verb/method based upon _method or falling
@@ -78,18 +77,19 @@ class Server {
7877
const method = request.input('_method', request.method())
7978
const resolvedRoute = this.Route.resolve(requestUrl, method, request.hostname())
8079
request._params = resolvedRoute.params
81-
this.log.verbose('request on url %s ', req.url)
8280

83-
/**
84-
* @description final method to call after executing
85-
* global middleware
86-
* @method finalHandler
87-
* @return {Function}
88-
*/
8981
const finalHandler = function * () {
9082
self._finalHandler(resolvedRoute, request, response)
9183
}
92-
helpers.respondRequest(this.middleware, request, response, finalHandler)
84+
85+
this._staticHandler(request, response)
86+
.catch((e) => {
87+
if (e.status === 404) {
88+
helpers.respondRequest(this.middleware, request, response, finalHandler)
89+
return
90+
}
91+
helpers.handleRequestError(e, request, response)
92+
})
9393
}
9494

9595
/**

test/unit/server.spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ class Session {
2525
}
2626
const Config = {
2727
get: function (key) {
28-
return key === 'http.trustProxy' ? true : 2
28+
if(key === 'http.trustProxy') {
29+
return true
30+
} else if(key === 'static.indexFile') {
31+
return 'index.html'
32+
} else {
33+
return 2
34+
}
2935
}
3036
}
3137

@@ -69,6 +75,14 @@ describe("Server", function () {
6975
yield supertest(testServer).get('/foo.css').expect(404).end()
7076
})
7177

78+
it("should serve static resource even if route is defined", function * () {
79+
Route.get('/favicon.ico', function * (request, response) {
80+
response.send({rendered:true})
81+
})
82+
const testServer = http.createServer(this.server.handle.bind(this.server))
83+
yield supertest(testServer).get('/favicon.ico').expect('Content-type',/x-icon/).expect(200).end()
84+
})
85+
7286
it("should call route action if defined", function * () {
7387
Route.get('/', function * (request, response) {
7488
response.send({rendered:true})

0 commit comments

Comments
 (0)