Skip to content

Commit cf21428

Browse files
404 error
1 parent 4158805 commit cf21428

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uquik",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "uQuik HTTP(S) framework",
55
"main": "index.js",
66
"scripts": {
@@ -9,7 +9,7 @@
99
"profile": "node --prof examples/basic.js",
1010
"example": "node examples/basic.js",
1111
"test": "echo \"Error: no test specified\" && exit 1",
12-
"wrk": "wrk -t4 -c2500 -d60s -R62000 http://127.0.0.1:5000",
12+
"wrk": "wrk -t4 -c2500 -d60s http://127.0.0.1:5000",
1313
"wrk-post": "wrk -s examples/post.lua -t4 -c2500 -d60s -R31000 http://127.0.0.1:5000"
1414
},
1515
"dependencies": {

src/Server.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Server extends Router {
6060
this._routes_locked = false
6161

6262
this.handlers = new Map([
63-
['on_not_found', null],
63+
['on_not_found', (request, response) => response.status(404).send()],
6464
['on_error', (request, response, error) => {
6565
if (process.env.NODE_ENV === 'development') console.log(error)
6666

@@ -150,6 +150,10 @@ class Server extends Router {
150150
* @returns {Promise} Promise
151151
*/
152152
listen (port, host = '127.0.0.1') {
153+
// Adding not found handler (404)
154+
this.any('/*', (request, response) => this.handlers.get('on_not_found')(request, response))
155+
this.routes_locked = true
156+
153157
return new Promise((resolve, reject) =>
154158
this.uws_instance.listen(host, port, (listenSocket) => {
155159
if (listenSocket) {
@@ -222,20 +226,7 @@ class Server extends Router {
222226
if (typeof handler !== 'function') throw new Error('handler must be a function')
223227

224228
// Store not_found handler and bind it as a catchall route
225-
if (this.handlers.get('on_not_found') === null) {
226-
this.handlers.set('on_not_found', handler)
227-
return setTimeout(
228-
(reference) => {
229-
reference.any('/*', (request, response) => reference.handlers.get('on_not_found')(request, response))
230-
reference.routes_locked = true
231-
},
232-
0,
233-
this
234-
)
235-
}
236-
237-
// Do not allow user to re-register not found handler
238-
throw new Error('A Not Found handler has already been registered.')
229+
this.handlers.set('on_not_found', handler)
239230
}
240231

241232
/**

0 commit comments

Comments
 (0)