Skip to content

Commit 0405db1

Browse files
FrandoEomm
authored andcommitted
feature: pass route params to handlers (#29)
1 parent b16f280 commit 0405db1

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ fastify.listen(3000, err => {
7878

7979
In this case there won't be any global handler, so it will respond with a 404 error on every unregistered route, closing the incoming upgrade connection requests.
8080

81+
The route handler receives route params as a third argument:
82+
83+
```js
84+
fastify.get('/ws/:id', { websocket: true }, (connection, req, params) => {
85+
connection.write(`hi on stream ${params.id}`)
86+
})
87+
```
88+
8189
However you can still pass a default global handler, that will be used as default handler.
8290

8391
```js

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ function fastifyWebsocket (fastify, opts, next) {
4646
throw new Error('invalid wsHandler function')
4747
}
4848

49-
router.on('GET', routeOptions.path, (req, _) => {
50-
const result = wsHandler(req[kWs], req)
49+
router.on('GET', routeOptions.path, (req, _, params) => {
50+
const result = wsHandler(req[kWs], req, params)
5151

5252
if (result && typeof result.catch === 'function') {
5353
result.catch((err) => req[kWs].destroy(err))

test/router.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,48 @@ test(`Should return 404 on http request`, t => {
286286
t.end()
287287
})
288288
})
289+
290+
test('Should pass route params to handlers', t => {
291+
const fastify = Fastify()
292+
293+
t.tearDown(() => fastify.close())
294+
295+
fastify.register(fastifyWebsocket)
296+
fastify.get('/ws', { websocket: true }, (conn, req, params) => {
297+
t.equal(Object.keys(params).length, 0, 'params are empty')
298+
conn.write('empty')
299+
conn.end()
300+
})
301+
fastify.get('/ws/:id', { websocket: true }, (conn, req, params) => {
302+
t.equal(params.id, 'foo', 'params are correct')
303+
conn.write(params.id)
304+
conn.end()
305+
})
306+
307+
fastify.listen(0, err => {
308+
let pending = 2
309+
t.error(err)
310+
const client = websocket(
311+
'ws://localhost:' + (fastify.server.address()).port + '/ws/foo'
312+
)
313+
const client2 = websocket(
314+
'ws://localhost:' + (fastify.server.address()).port + '/ws'
315+
)
316+
t.tearDown(client.destroy.bind(client))
317+
t.tearDown(client2.destroy.bind(client))
318+
319+
client.setEncoding('utf8')
320+
client2.setEncoding('utf8')
321+
322+
client.once('data', chunk => {
323+
t.equal(chunk, 'foo')
324+
client.end()
325+
if (--pending === 0) t.end()
326+
})
327+
client2.once('data', chunk => {
328+
t.equal(chunk, 'empty')
329+
client2.end()
330+
if (--pending === 0) t.end()
331+
})
332+
})
333+
})

0 commit comments

Comments
 (0)