@fastify/express 4.0.1 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,7 @@ on:
17
17
 
18
18
  jobs:
19
19
  test:
20
- uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5.0.0
20
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
21
21
  with:
22
22
  lint: true
23
23
  license-check: true
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # @fastify/express
2
2
 
3
- ![CI](https://github.com/fastify/fastify-express/workflows/CI/badge.svg)
3
+ [![CI](https://github.com/fastify/fastify-express/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-express/actions/workflows/ci.yml)
4
4
  [![NPM version](https://img.shields.io/npm/v/@fastify/express.svg?style=flat)](https://www.npmjs.com/package/@fastify/express)
5
- [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
5
+ [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
6
6
 
7
7
  This plugin adds full [Express](http://expressjs.com) compatibility to Fastify, it exposes the same `use` function of Express, and it allows you to use any Express middleware or application.<br/>
8
8
 
@@ -13,7 +13,7 @@ This plugin adds full [Express](http://expressjs.com) compatibility to Fastify,
13
13
  <td><b>This plugin should not be used as a long-term solution, it aims to help you have a smooth transition from Express to Fastify, but you should migrate your Express specific code to Fastify over time.</b></td>
14
14
  </tr>
15
15
  <tr>
16
- <td><b>Since <a href="https://github.com/expressjs/express/issues/2761">Express does not support Node.js core HTTP/2 module</a>, this plugin does not support HTTP/2 too.</b></td>
16
+ <td><b>Since <a href="https://github.com/expressjs/express/issues/2761">Express does not support Node.js core HTTP/2 module</a>, this plugin does not support HTTP/2 either.</b></td>
17
17
  </tr>
18
18
  <tbody>
19
19
  </table>
@@ -23,6 +23,19 @@ This plugin adds full [Express](http://expressjs.com) compatibility to Fastify,
23
23
  npm i @fastify/express
24
24
  ```
25
25
 
26
+ ### Compatibility
27
+ | Plugin version | Fastify version |
28
+ | ---------------|-----------------|
29
+ | `^4.x` | `^5.x` |
30
+ | `^2.x` | `^4.x` |
31
+ | `^1.x` | `^3.x` |
32
+ | `^0.x` | `^2.x` |
33
+
34
+
35
+ Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin
36
+ in the table above.
37
+ See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.
38
+
26
39
  ## Usage
27
40
  Register the plugin and start using your Express middlewares.
28
41
  ```js
@@ -105,10 +118,10 @@ me@computer ~ % curl -X GET http://localhost:3000/foo
105
118
  {"foo":"bar"}%
106
119
  me@computer ~ % curl -X GET http://localhost:3000/bar
107
120
  {"msg":"not found"}%
108
- me@computer ~ % curl -X PATCH -H 'content-type:application/json' http://localhost:3000/bar
121
+ me@computer ~ % curl -X PATCH -H 'content-type:application/json' http://localhost:3000/bar
109
122
  {"msg":"no req.body"}%
110
123
  me@computer ~ % curl -X PATCH -H 'content-type:application/json' -d '{"foo2":"bar2"}' http://localhost:3000/bar
111
- {"foo2":"bar2"}%
124
+ {"foo2":"bar2"}%
112
125
  ```
113
126
 
114
127
  ### Encapsulation support
@@ -140,9 +153,9 @@ async function subsystem (fastify, opts) {
140
153
  }
141
154
  ```
142
155
 
143
- ### Hooks and middlewares
156
+ ### Hooks and middleware
144
157
 
145
- Every registered middleware will be run during the `onRequest` hook phase, so the registration order is important.
158
+ Every registered middleware will be run during the `onRequest` hook phase, so the registration order is important.
146
159
  Take a look at the [Lifecycle](https://fastify.dev/docs/latest/Reference/Lifecycle) documentation page to understand better how every request is executed.
147
160
 
148
161
  ```js
@@ -170,7 +183,7 @@ async function subsystem (fastify, opts) {
170
183
 
171
184
  ### Restrict middleware execution to a certain path(s)
172
185
 
173
- If you need to run a middleware only under certain path(s), just pass the path as first parameter to use and you are done!
186
+ If you need to run a middleware only under certain path(s), just pass the path as the first parameter to use and you are done!
174
187
 
175
188
  ```js
176
189
  const fastify = require('fastify')()
@@ -197,7 +210,7 @@ async function subsystem (fastify, opts) {
197
210
 
198
211
  It is possible to wrap the Express request object in a Proxy by passing `createProxyHandler` function to generate the Proxy handler. The function will receive the Fastify request object as the first parameter.
199
212
 
200
- For example using Proxy to expose something from Fastify request into the Express request.
213
+ For example, using Proxy to expose something from Fastify request into the Express request.
201
214
 
202
215
  ```js
203
216
  fastify.decorateRequest('welcomeMessage', 'Hello World');
@@ -220,9 +233,9 @@ To use this module with TypeScript, make sure to install `@types/express`.
220
233
 
221
234
  You will need to add `"types": ["@fastify/express"]` to your tsconfig.json file when using `require` to import the plugin.
222
235
 
223
- ## Middlewares alternatives
236
+ ## Middleware alternatives
224
237
 
225
- Fastify offers some alternatives to the most commonly used middlewares, following, you can find a list.
238
+ Fastify offers some alternatives to the most commonly used middleware:
226
239
 
227
240
  | Express Middleware | Fastify Plugin |
228
241
  | ------------- |---------------|
@@ -235,7 +248,7 @@ Fastify offers some alternatives to the most commonly used middlewares, followin
235
248
  ### POST request with body hangs up
236
249
 
237
250
  [body-parser](https://github.com/expressjs/body-parser) library incompatible with `fastify-express`, when you have `fastify` routes and any `express` middlewares.
238
- Any POST requests with **body**, which `body-parser` will try to parse, will be hangs up.
251
+ Any POST requests with **body**, which `body-parser` will try to parse, will hang up.
239
252
 
240
253
  Example application:
241
254
 
@@ -0,0 +1,6 @@
1
+ 'use strict'
2
+
3
+ module.exports = require('neostandard')({
4
+ ignores: require('neostandard').resolveIgnoresFromGitignore(),
5
+ ts: true
6
+ })
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@fastify/express",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "Express compatibility layer for Fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
7
7
  "types": "types/index.d.ts",
8
8
  "scripts": {
9
- "lint": "standard",
9
+ "lint": "eslint",
10
+ "lint:fix": "eslint --fix",
10
11
  "test": "npm run test:unit && npm run test:typescript",
11
12
  "test:unit": "tap",
12
13
  "test:typescript": "tsd"
@@ -23,24 +24,54 @@
23
24
  "compatibility"
24
25
  ],
25
26
  "author": "Tomas Della Vedova - @delvedor (http://delved.org)",
27
+ "contributors": [
28
+ {
29
+ "name": "Matteo Collina",
30
+ "email": "hello@matteocollina.com"
31
+ },
32
+ {
33
+ "name": "Manuel Spigolon",
34
+ "email": "behemoth89@gmail.com"
35
+ },
36
+ {
37
+ "name": "James Sumners",
38
+ "url": "https://james.sumners.info"
39
+ },
40
+ {
41
+ "name": "Frazer Smith",
42
+ "email": "frazer.dev@icloud.com",
43
+ "url": "https://github.com/fdawgs"
44
+ }
45
+ ],
26
46
  "license": "MIT",
27
47
  "bugs": {
28
48
  "url": "https://github.com/fastify/fastify-express/issues"
29
49
  },
30
50
  "homepage": "https://github.com/fastify/fastify-express#readme",
51
+ "funding": [
52
+ {
53
+ "type": "github",
54
+ "url": "https://github.com/sponsors/fastify"
55
+ },
56
+ {
57
+ "type": "opencollective",
58
+ "url": "https://opencollective.com/fastify"
59
+ }
60
+ ],
31
61
  "devDependencies": {
32
62
  "@fastify/formbody": "^8.0.0",
33
- "@types/express": "^4.17.21",
63
+ "@types/express": "^5.0.0",
34
64
  "@types/node": "*",
35
65
  "body-parser": "^1.20.2",
36
66
  "cors": "^2.8.5",
67
+ "eslint": "^9.17.0",
37
68
  "fastify": "^5.0.0",
38
- "helmet": "^7.1.0",
69
+ "helmet": "^8.0.0",
70
+ "neostandard": "^0.12.0",
39
71
  "passport": "^0.7.0",
40
72
  "passport-http-bearer": "^1.0.1",
41
73
  "serve-static": "^1.15.0",
42
74
  "simple-get": "^4.0.1",
43
- "standard": "^17.1.0",
44
75
  "tap": "^21.0.0",
45
76
  "tsd": "^0.31.0"
46
77
  },
package/test/404s.test.js CHANGED
@@ -14,33 +14,33 @@ test('run hooks and middleware on default 404', t => {
14
14
  fastify
15
15
  .register(expressPlugin)
16
16
  .after(() => {
17
- fastify.use(function (req, res, next) {
17
+ fastify.use(function (_req, _res, next) {
18
18
  t.pass('middleware called')
19
19
  next()
20
20
  })
21
21
  })
22
22
 
23
- fastify.addHook('onRequest', function (req, res, next) {
23
+ fastify.addHook('onRequest', function (_req, _res, next) {
24
24
  t.pass('onRequest called')
25
25
  next()
26
26
  })
27
27
 
28
- fastify.addHook('preHandler', function (request, reply, next) {
28
+ fastify.addHook('preHandler', function (_request, _reply, next) {
29
29
  t.pass('preHandler called')
30
30
  next()
31
31
  })
32
32
 
33
- fastify.addHook('onSend', function (request, reply, payload, next) {
33
+ fastify.addHook('onSend', function (_request, _reply, _payload, next) {
34
34
  t.pass('onSend called')
35
35
  next()
36
36
  })
37
37
 
38
- fastify.addHook('onResponse', function (request, reply, next) {
38
+ fastify.addHook('onResponse', function (_request, _reply, next) {
39
39
  t.pass('onResponse called')
40
40
  next()
41
41
  })
42
42
 
43
- fastify.get('/', function (req, reply) {
43
+ fastify.get('/', function (_req, reply) {
44
44
  reply.send({ hello: 'world' })
45
45
  })
46
46
 
@@ -54,7 +54,7 @@ test('run hooks and middleware on default 404', t => {
54
54
  url: 'http://localhost:' + fastify.server.address().port,
55
55
  body: JSON.stringify({ hello: 'world' }),
56
56
  headers: { 'Content-Type': 'application/json' }
57
- }, (err, response, body) => {
57
+ }, (err, response) => {
58
58
  t.error(err)
59
59
  t.equal(response.statusCode, 404)
60
60
  })
@@ -68,28 +68,28 @@ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
68
68
  t.teardown(fastify.close)
69
69
  fastify.register(expressPlugin)
70
70
 
71
- fastify.register(fp(function (instance, options, next) {
72
- instance.addHook('onRequest', function (req, res, next) {
71
+ fastify.register(fp(function (instance, _options, next) {
72
+ instance.addHook('onRequest', function (_req, _res, next) {
73
73
  t.pass('onRequest called')
74
74
  next()
75
75
  })
76
76
 
77
- instance.use(function (req, res, next) {
77
+ instance.use(function (_req, _res, next) {
78
78
  t.pass('middleware called')
79
79
  next()
80
80
  })
81
81
 
82
- instance.addHook('preHandler', function (request, reply, next) {
82
+ instance.addHook('preHandler', function (_request, _reply, next) {
83
83
  t.pass('preHandler called')
84
84
  next()
85
85
  })
86
86
 
87
- instance.addHook('onSend', function (request, reply, payload, next) {
87
+ instance.addHook('onSend', function (_request, _reply, _payload, next) {
88
88
  t.pass('onSend called')
89
89
  next()
90
90
  })
91
91
 
92
- instance.addHook('onResponse', function (request, reply, next) {
92
+ instance.addHook('onResponse', function (_request, _reply, next) {
93
93
  t.pass('onResponse called')
94
94
  next()
95
95
  })
@@ -97,7 +97,7 @@ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
97
97
  next()
98
98
  }))
99
99
 
100
- fastify.get('/', function (req, reply) {
100
+ fastify.get('/', function (_req, reply) {
101
101
  reply.send({ hello: 'world' })
102
102
  })
103
103
 
@@ -108,7 +108,7 @@ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
108
108
  url: address,
109
109
  body: JSON.stringify({ hello: 'world' }),
110
110
  headers: { 'Content-Type': 'application/json' }
111
- }, (err, response, body) => {
111
+ }, (err, response) => {
112
112
  t.error(err)
113
113
  t.equal(response.statusCode, 404)
114
114
  })
@@ -122,28 +122,28 @@ test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
122
122
  t.teardown(fastify.close)
123
123
  fastify.register(expressPlugin)
124
124
 
125
- const plugin = fp((instance, opts, next) => {
126
- instance.addHook('onRequest', function (req, res, next) {
125
+ const plugin = fp((instance, _opts, next) => {
126
+ instance.addHook('onRequest', function (_req, _res, next) {
127
127
  t.pass('onRequest called')
128
128
  next()
129
129
  })
130
130
 
131
- instance.use(function (req, res, next) {
131
+ instance.use(function (_req, _res, next) {
132
132
  t.pass('middleware called')
133
133
  next()
134
134
  })
135
135
 
136
- instance.addHook('preHandler', function (request, reply, next) {
136
+ instance.addHook('preHandler', function (_request, _reply, next) {
137
137
  t.pass('preHandler called')
138
138
  next()
139
139
  })
140
140
 
141
- instance.addHook('onSend', function (request, reply, payload, next) {
141
+ instance.addHook('onSend', function (_request, _reply, _payload, next) {
142
142
  t.pass('onSend called')
143
143
  next()
144
144
  })
145
145
 
146
- instance.addHook('onResponse', function (request, reply, next) {
146
+ instance.addHook('onResponse', function (_request, _reply, next) {
147
147
  t.pass('onResponse called')
148
148
  next()
149
149
  })
@@ -153,11 +153,11 @@ test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
153
153
 
154
154
  fastify.register(plugin)
155
155
 
156
- fastify.get('/', function (req, reply) {
156
+ fastify.get('/', function (_req, reply) {
157
157
  reply.send({ hello: 'world' })
158
158
  })
159
159
 
160
- fastify.setNotFoundHandler(function (req, reply) {
160
+ fastify.setNotFoundHandler(function (_req, reply) {
161
161
  reply.code(404).send('this was not found')
162
162
  })
163
163
 
@@ -184,58 +184,58 @@ test('run hooks and middleware with encapsulated 404', t => {
184
184
  fastify
185
185
  .register(expressPlugin)
186
186
  .after(() => {
187
- fastify.use(function (req, res, next) {
187
+ fastify.use(function (_req, _res, next) {
188
188
  t.pass('middleware called')
189
189
  next()
190
190
  })
191
191
  })
192
192
 
193
- fastify.addHook('onRequest', function (req, res, next) {
193
+ fastify.addHook('onRequest', function (_req, _res, next) {
194
194
  t.pass('onRequest called')
195
195
  next()
196
196
  })
197
197
 
198
- fastify.addHook('preHandler', function (request, reply, next) {
198
+ fastify.addHook('preHandler', function (_request, _reply, next) {
199
199
  t.pass('preHandler called')
200
200
  next()
201
201
  })
202
202
 
203
- fastify.addHook('onSend', function (request, reply, payload, next) {
203
+ fastify.addHook('onSend', function (_request, _reply, _payload, next) {
204
204
  t.pass('onSend called')
205
205
  next()
206
206
  })
207
207
 
208
- fastify.addHook('onResponse', function (request, reply, next) {
208
+ fastify.addHook('onResponse', function (_request, _reply, next) {
209
209
  t.pass('onResponse called')
210
210
  next()
211
211
  })
212
212
 
213
- fastify.register(function (f, opts, next) {
214
- f.setNotFoundHandler(function (req, reply) {
213
+ fastify.register(function (f, _opts, next) {
214
+ f.setNotFoundHandler(function (_req, reply) {
215
215
  reply.code(404).send('this was not found 2')
216
216
  })
217
217
 
218
- f.addHook('onRequest', function (req, res, next) {
218
+ f.addHook('onRequest', function (_req, _res, next) {
219
219
  t.pass('onRequest 2 called')
220
220
  next()
221
221
  })
222
222
 
223
- f.use(function (req, res, next) {
223
+ f.use(function (_req, _res, next) {
224
224
  t.pass('middleware 2 called')
225
225
  next()
226
226
  })
227
227
 
228
- f.addHook('preHandler', function (request, reply, next) {
228
+ f.addHook('preHandler', function (_request, _reply, next) {
229
229
  t.pass('preHandler 2 called')
230
230
  next()
231
231
  })
232
232
 
233
- f.addHook('onSend', function (request, reply, payload, next) {
233
+ f.addHook('onSend', function (_request, _reply, _payload, next) {
234
234
  t.pass('onSend 2 called')
235
235
  next()
236
236
  })
237
237
 
238
- f.addHook('onResponse', function (request, reply, next) {
238
+ f.addHook('onResponse', function (_request, _reply, next) {
239
239
  t.pass('onResponse 2 called')
240
240
  next()
241
241
  })
@@ -253,7 +253,7 @@ test('run hooks and middleware with encapsulated 404', t => {
253
253
  url: 'http://localhost:' + fastify.server.address().port + '/test',
254
254
  body: JSON.stringify({ hello: 'world' }),
255
255
  headers: { 'Content-Type': 'application/json' }
256
- }, (err, response, body) => {
256
+ }, (err, response) => {
257
257
  t.error(err)
258
258
  t.equal(response.statusCode, 404)
259
259
  })
@@ -267,13 +267,13 @@ test('run middlewares on default 404', t => {
267
267
  fastify
268
268
  .register(expressPlugin)
269
269
  .after(() => {
270
- fastify.use(function (req, res, next) {
270
+ fastify.use(function (_req, _res, next) {
271
271
  t.pass('middleware called')
272
272
  next()
273
273
  })
274
274
  })
275
275
 
276
- fastify.get('/', function (req, reply) {
276
+ fastify.get('/', function (_req, reply) {
277
277
  reply.send({ hello: 'world' })
278
278
  })
279
279
 
@@ -287,7 +287,7 @@ test('run middlewares on default 404', t => {
287
287
  url: 'http://localhost:' + fastify.server.address().port,
288
288
  body: JSON.stringify({ hello: 'world' }),
289
289
  headers: { 'Content-Type': 'application/json' }
290
- }, (err, response, body) => {
290
+ }, (err, response) => {
291
291
  t.error(err)
292
292
  t.equal(response.statusCode, 404)
293
293
  })
@@ -301,18 +301,18 @@ test('run middlewares with encapsulated 404', t => {
301
301
  fastify
302
302
  .register(expressPlugin)
303
303
  .after(() => {
304
- fastify.use(function (req, res, next) {
304
+ fastify.use(function (_req, _res, next) {
305
305
  t.pass('middleware called')
306
306
  next()
307
307
  })
308
308
  })
309
309
 
310
- fastify.register(function (f, opts, next) {
311
- f.setNotFoundHandler(function (req, reply) {
310
+ fastify.register(function (f, _opts, next) {
311
+ f.setNotFoundHandler(function (_req, reply) {
312
312
  reply.code(404).send('this was not found 2')
313
313
  })
314
314
 
315
- f.use(function (req, res, next) {
315
+ f.use(function (_req, _res, next) {
316
316
  t.pass('middleware 2 called')
317
317
  next()
318
318
  })
@@ -330,7 +330,7 @@ test('run middlewares with encapsulated 404', t => {
330
330
  url: 'http://localhost:' + fastify.server.address().port + '/test',
331
331
  body: JSON.stringify({ hello: 'world' }),
332
332
  headers: { 'Content-Type': 'application/json' }
333
- }, (err, response, body) => {
333
+ }, (err, response) => {
334
334
  t.error(err)
335
335
  t.equal(response.statusCode, 404)
336
336
  })
@@ -13,12 +13,12 @@ test('Register express application', t => {
13
13
  const express = Express()
14
14
  t.teardown(fastify.close)
15
15
 
16
- express.use(function (req, res, next) {
16
+ express.use(function (_req, res, next) {
17
17
  res.setHeader('x-custom', true)
18
18
  next()
19
19
  })
20
20
 
21
- express.get('/hello', (req, res) => {
21
+ express.get('/hello', (_req, res) => {
22
22
  res.status(201)
23
23
  res.json({ hello: 'world' })
24
24
  })
@@ -47,17 +47,17 @@ test('Register express application that uses Router', t => {
47
47
 
48
48
  const router = Express.Router()
49
49
 
50
- router.use(function (req, res, next) {
50
+ router.use(function (_req, res, next) {
51
51
  res.setHeader('x-custom', true)
52
52
  next()
53
53
  })
54
54
 
55
- router.get('/hello', (req, res) => {
55
+ router.get('/hello', (_req, res) => {
56
56
  res.status(201)
57
57
  res.json({ hello: 'world' })
58
58
  })
59
59
 
60
- router.get('/foo', (req, res) => {
60
+ router.get('/foo', (_req, res) => {
61
61
  res.status(400)
62
62
  res.json({ foo: 'bar' })
63
63
  })
@@ -95,7 +95,7 @@ test('Should remove x-powered-by header', t => {
95
95
 
96
96
  const router = Express.Router()
97
97
 
98
- router.get('/', (req, res) => {
98
+ router.get('/', (_req, res) => {
99
99
  res.status(201)
100
100
  res.json({ hello: 'world' })
101
101
  })
@@ -123,7 +123,7 @@ test('Should expose the express app on the fastify instance', t => {
123
123
 
124
124
  const router = Express.Router()
125
125
 
126
- router.get('/', (req, res) => {
126
+ router.get('/', (_req, res) => {
127
127
  res.status(201)
128
128
  res.json({ hello: 'world' })
129
129
  })
@@ -137,7 +137,7 @@ test('Should expose the express app on the fastify instance', t => {
137
137
  sget({
138
138
  method: 'GET',
139
139
  url: address
140
- }, (err, res) => {
140
+ }, (err) => {
141
141
  t.error(err)
142
142
  t.equal(fastify.express.disabled('x-powered-by'), true)
143
143
  })
@@ -157,7 +157,7 @@ test('Should flush headers if express handles request', t => {
157
157
 
158
158
  const router = Express.Router()
159
159
 
160
- router.get('/', (req, res) => {
160
+ router.get('/', (_req, res) => {
161
161
  res.status(201)
162
162
  res.json({ hello: 'world' })
163
163
  })