@fastify/express 1.0.0 → 1.1.0

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.
package/README.md CHANGED
@@ -1,18 +1,27 @@
1
- # fastify-express
1
+ # @fastify/express
2
2
 
3
3
  ![CI](https://github.com/fastify/fastify-express/workflows/CI/badge.svg)
4
- [![NPM version](https://img.shields.io/npm/v/fastify-express.svg?style=flat)](https://www.npmjs.com/package/fastify-express)
4
+ [![NPM version](https://img.shields.io/npm/v/@fastify/express.svg?style=flat)](https://www.npmjs.com/package/@fastify/express)
5
5
  [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-express/badge.svg)](https://snyk.io/test/github/fastify/fastify-express)
6
6
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
7
7
 
8
8
  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/>
9
9
 
10
- | **Note** | 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. |
11
- | ---- | :---- |
10
+ <table>
11
+ <tbody>
12
+ <tr>
13
+ <td rowspan="2"><b>Note</b></td>
14
+ <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>
15
+ </tr>
16
+ <tr>
17
+ <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>
18
+ </tr>
19
+ <tbody>
20
+ </table>
12
21
 
13
22
  ## Install
14
23
  ```
15
- npm i fastify-express
24
+ npm i @fastify/express
16
25
  ```
17
26
 
18
27
  ## Usage
@@ -22,7 +31,7 @@ const Fastify = require('fastify')
22
31
 
23
32
  async function build () {
24
33
  const fastify = Fastify()
25
- await fastify.register(require('fastify-express'))
34
+ await fastify.register(require('@fastify/express'))
26
35
  // do you know we also have cors support?
27
36
  // https://github.com/fastify/fastify-cors
28
37
  fastify.use(require('cors')())
@@ -38,7 +47,7 @@ build()
38
47
 
39
48
  ### Add a complete application
40
49
 
41
- You can register an entire Express application and make it work with Fastify. Remember, `fastify-express` is just `express` under the covers and requires the same body parsers as you'd use in `express`.
50
+ You can register an entire Express application and make it work with Fastify. Remember, `@fastify/express` is just `express` under the covers and requires the same body parsers as you'd use in `express`.
42
51
 
43
52
  ```js
44
53
  // index.js
@@ -76,7 +85,7 @@ router.use('*', (req, res) => {
76
85
  res.json({ msg: 'not found'})
77
86
  })
78
87
 
79
- fastify.register(require('fastify-express'))
88
+ fastify.register(require('@fastify/express'))
80
89
  .after(() => {
81
90
  fastify.use(express.urlencoded({extended: false})) // for Postman x-www-form-urlencoded
82
91
  fastify.use(express.json())
@@ -114,7 +123,7 @@ const fastify = require('fastify')()
114
123
  fastify.register(subsystem)
115
124
 
116
125
  async function subsystem (fastify, opts) {
117
- await fastify.register(require('fastify-express'))
126
+ await fastify.register(require('@fastify/express'))
118
127
  fastify.use(require('cors')())
119
128
  }
120
129
  ```
@@ -124,7 +133,7 @@ async function subsystem (fastify, opts) {
124
133
  const fastify = require('fastify')()
125
134
 
126
135
  fastify
127
- .register(require('fastify-express'))
136
+ .register(require('@fastify/express'))
128
137
  .register(subsystem)
129
138
 
130
139
  async function subsystem (fastify, opts) {
@@ -141,7 +150,7 @@ Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Lifecycle/) do
141
150
  const fastify = require('fastify')()
142
151
 
143
152
  fastify
144
- .register(require('fastify-express'))
153
+ .register(require('@fastify/express'))
145
154
  .register(subsystem)
146
155
 
147
156
  async function subsystem (fastify, opts) {
@@ -170,7 +179,7 @@ const path = require('path')
170
179
  const serveStatic = require('serve-static')
171
180
 
172
181
  fastify
173
- .register(require('fastify-express'))
182
+ .register(require('@fastify/express'))
174
183
  .register(subsystem)
175
184
 
176
185
  async function subsystem (fastify, opts) {
@@ -189,7 +198,7 @@ async function subsystem (fastify, opts) {
189
198
 
190
199
  To use this module with TypeScript, make sure to install `@types/express`.
191
200
 
192
- You will need to add `"types": ["fastify-express"]` to your tsconfig.json file when using `require` to import the plugin.
201
+ You will need to add `"types": ["@fastify/express"]` to your tsconfig.json file when using `require` to import the plugin.
193
202
 
194
203
  ## Middlewares alternatives
195
204
 
@@ -197,9 +206,65 @@ Fastify offers some alternatives to the most commonly used middlewares, followin
197
206
 
198
207
  | Express Middleware | Fastify Plugin |
199
208
  | ------------- |---------------|
200
- | [`helmet`](https://github.com/helmetjs/helmet) | [`fastify-helmet`](https://github.com/fastify/fastify-helmet) |
201
- | [`cors`](https://github.com/expressjs/cors) | [`fastify-cors`](https://github.com/fastify/fastify-cors) |
202
- | [`serve-static`](https://github.com/expressjs/serve-static) | [`fastify-static`](https://github.com/fastify/fastify-static) |
209
+ | [`helmet`](https://github.com/helmetjs/helmet) | [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) |
210
+ | [`cors`](https://github.com/expressjs/cors) | [`@fastify/cors`](https://github.com/fastify/fastify-cors) |
211
+ | [`serve-static`](https://github.com/expressjs/serve-static) | [`@fastify/static`](https://github.com/fastify/fastify-static) |
212
+
213
+ ## Troubleshooting
214
+
215
+ ### POST request with body hangs up
216
+
217
+ [body-parser](https://github.com/expressjs/body-parser) library incompatible with `fastify-express`, when you have `fastify` routes and any `express` middlewares.
218
+ Any POST requests with **body**, which `body-parser` will try to parse, will be hangs up.
219
+
220
+ Example application:
221
+
222
+ ```js
223
+ const Fastify = require('fastify')
224
+ const Express = require('express')
225
+ const expressPlugin = require('fastify-express')
226
+ const bodyParser = require('body-parser')
227
+
228
+ const fastify = Fastify()
229
+ const express = Express()
230
+
231
+ express.use(bodyParser.urlencoded({ extended: false }))
232
+
233
+ await fastify.register(expressPlugin)
234
+
235
+ fastify.use(express)
236
+
237
+ // this route will never reply
238
+ fastify.post('/hello', (req, reply) => {
239
+ return { hello: 'world' }
240
+ })
241
+ ```
242
+
243
+ For this case, you need to remove `body-parser`, install `fastify-formbody` and change `fastify-express` options:
244
+
245
+
246
+ ```js
247
+ const Fastify = require('fastify')
248
+ const Express = require('express')
249
+ const expressPlugin = require('fastify-express')
250
+ const fastifyFormBody = require('fastify-formbody')
251
+
252
+ const fastify = Fastify()
253
+ const express = Express()
254
+
255
+ await fastify.register(fastifyFormBody)
256
+ await fastify.register(expressPlugin, {
257
+ // run express after `fastify-formbody` logic
258
+ expressHook: 'preHandler'
259
+ })
260
+
261
+ fastify.use(express)
262
+
263
+ // it works!
264
+ fastify.post('/hello', (req, reply) => {
265
+ return { hello: 'world' }
266
+ })
267
+ ```
203
268
 
204
269
  ## License
205
270
 
package/index.js CHANGED
@@ -6,14 +6,18 @@ const Express = require('express')
6
6
  const kMiddlewares = Symbol('fastify-express-middlewares')
7
7
 
8
8
  function expressPlugin (fastify, options, next) {
9
+ const {
10
+ expressHook = 'onRequest'
11
+ } = options
12
+
9
13
  fastify.decorate('use', use)
10
14
  fastify[kMiddlewares] = []
11
15
  fastify.decorate('express', Express())
12
16
  fastify.express.disable('x-powered-by')
13
17
 
14
18
  fastify
15
- .addHook('onRequest', enhanceRequest)
16
- .addHook('onRequest', runConnect)
19
+ .addHook(expressHook, enhanceRequest)
20
+ .addHook(expressHook, runConnect)
17
21
  .addHook('onRegister', onRegister)
18
22
 
19
23
  function use (path, fn) {
@@ -39,6 +43,15 @@ function expressPlugin (fastify, options, next) {
39
43
  req.raw.log = req.log
40
44
  reply.raw.log = req.log
41
45
 
46
+ // backward compatibility for body-parser
47
+ if (req.body) {
48
+ req.raw.body = req.body
49
+ }
50
+ // backward compatibility for cookie-parser
51
+ if (req.cookies) {
52
+ req.raw.cookies = req.cookies
53
+ }
54
+
42
55
  const originalProtocol = req.raw.protocol
43
56
  // Make it lazy as it does a bit of work
44
57
  Object.defineProperty(req.raw, 'protocol', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/express",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Express compatibility layer for Fastify",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,8 +27,10 @@
27
27
  "devDependencies": {
28
28
  "@types/express": "^4.17.1",
29
29
  "@types/node": "*",
30
+ "body-parser": "^1.20.0",
30
31
  "cors": "^2.8.5",
31
32
  "fastify": "^3.0.0",
33
+ "fastify-formbody": "^5.3.0",
32
34
  "helmet": "^5.0.0",
33
35
  "passport": "^0.5.1",
34
36
  "passport-http-bearer": "^1.0.1",
@@ -0,0 +1,153 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const Fastify = require('fastify')
5
+ const fastifyFormBody = require('fastify-formbody')
6
+ const Express = require('express')
7
+ const bodyParser = require('body-parser')
8
+ const sget = require('simple-get').concat
9
+
10
+ const expressPlugin = require('../index')
11
+
12
+ test('POST request without form body works', t => {
13
+ t.plan(5)
14
+ const fastify = Fastify()
15
+ const express = Express()
16
+ t.teardown(fastify.close)
17
+
18
+ fastify.register(fastifyFormBody)
19
+ fastify.register(expressPlugin)
20
+ .after(() => {
21
+ express.use(bodyParser.urlencoded({ extended: false }))
22
+ fastify.use(express)
23
+ fastify.use((req, res, next) => {
24
+ // body-parser default value
25
+ t.deepEqual(req.body, {})
26
+ next()
27
+ })
28
+ })
29
+
30
+ fastify.post('/hello', (req, reply) => {
31
+ return { hello: 'world' }
32
+ })
33
+
34
+ fastify.listen(0, (err, address) => {
35
+ t.error(err)
36
+ sget({
37
+ method: 'post',
38
+ url: address + '/hello',
39
+ timeout: 100
40
+ }, (err, res, data) => {
41
+ t.error(err)
42
+ t.strictEqual(res.statusCode, 200)
43
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
44
+ })
45
+ })
46
+ })
47
+
48
+ test('POST request with form body and without body-parser works', t => {
49
+ t.plan(5)
50
+ const fastify = Fastify()
51
+ const express = Express()
52
+ t.teardown(fastify.close)
53
+
54
+ fastify.register(fastifyFormBody)
55
+ fastify.register(expressPlugin)
56
+ .after(() => {
57
+ fastify.use(express)
58
+ fastify.use((req, res, next) => {
59
+ // req.body default value
60
+ t.strictEqual(req.body, undefined)
61
+ next()
62
+ })
63
+ })
64
+
65
+ fastify.post('/hello', (req, reply) => {
66
+ return { hello: 'world' }
67
+ })
68
+
69
+ fastify.listen(0, (err, address) => {
70
+ t.error(err)
71
+ sget({
72
+ method: 'post',
73
+ url: address + '/hello',
74
+ form: { input: 'test' },
75
+ timeout: 100
76
+ }, (err, res, data) => {
77
+ t.error(err)
78
+ t.strictEqual(res.statusCode, 200)
79
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
80
+ })
81
+ })
82
+ })
83
+
84
+ test('POST request with form body and body-parser hangs up', t => {
85
+ t.plan(3)
86
+ const fastify = Fastify()
87
+ const express = Express()
88
+ t.teardown(fastify.close)
89
+
90
+ fastify.register(fastifyFormBody)
91
+ fastify.register(expressPlugin)
92
+ .after(() => {
93
+ express.use(bodyParser.urlencoded({ extended: false }))
94
+ fastify.use(express)
95
+ fastify.use((req, res, next) => {
96
+ // body-parser result
97
+ t.deepEqual(req.body, { input: 'test' })
98
+ next()
99
+ })
100
+ })
101
+
102
+ fastify.post('/hello', (req, reply) => {
103
+ return { hello: 'world' }
104
+ })
105
+
106
+ fastify.listen(0, (err, address) => {
107
+ t.error(err)
108
+ sget({
109
+ method: 'post',
110
+ url: address + '/hello',
111
+ form: { input: 'test' },
112
+ timeout: 100
113
+ }, (err, res, data) => {
114
+ t.strictEqual(err.message, 'Request timed out')
115
+ })
116
+ })
117
+ })
118
+
119
+ test('POST request with form body and body-parser hangs up, compatibility case', t => {
120
+ t.plan(5)
121
+ const fastify = Fastify()
122
+ const express = Express()
123
+ t.teardown(fastify.close)
124
+
125
+ fastify.register(fastifyFormBody)
126
+ fastify.register(expressPlugin, { expressHook: 'preHandler' })
127
+ .after(() => {
128
+ fastify.use(express)
129
+ fastify.use((req, res, next) => {
130
+ // fastify-formbody with backward compatibility result
131
+ t.deepEqual(req.body, { input: 'test' })
132
+ next()
133
+ })
134
+ })
135
+
136
+ fastify.post('/hello', (req, reply) => {
137
+ return { hello: 'world' }
138
+ })
139
+
140
+ fastify.listen(0, (err, address) => {
141
+ t.error(err)
142
+ sget({
143
+ method: 'post',
144
+ url: address + '/hello',
145
+ form: { input: 'test' },
146
+ timeout: 100
147
+ }, (err, res, data) => {
148
+ t.error(err)
149
+ t.strictEqual(res.statusCode, 200)
150
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
151
+ })
152
+ })
153
+ })