fastify
Advanced tools
Comparing version 0.25.3 to 0.26.0
@@ -1,2 +0,2 @@ | ||
'use string' | ||
'use strict' | ||
@@ -3,0 +3,0 @@ var Koa = require('koa') |
@@ -9,2 +9,4 @@ <h1 align="center">Fastify</h1> | ||
- `.header(name, value)` - Sets the headers. | ||
- `.type(value)` - Sets the header `Content-Type`. | ||
- `.redirect([code,] url)` - Redirect to the specified url, the status code is optional (default to `302`). | ||
- `.serializer(function)` - Sets a custom serializer for the payload. | ||
@@ -34,2 +36,9 @@ - `.send(payload)` - Sends the payload to the user, could be a plain text, JSON, stream, or an Error object. | ||
<a name="redirect"></a> | ||
### Redirect | ||
Redirects a request to the specified url, the status code is optional, default to `302`. | ||
```js | ||
reply.redirect('/home') | ||
``` | ||
<a name="type"></a> | ||
@@ -40,5 +49,3 @@ ### Type | ||
Example: | ||
``` | ||
```js | ||
reply.type('text/html') | ||
@@ -45,0 +52,0 @@ ``` |
@@ -276,3 +276,4 @@ 'use strict' | ||
RoutePrefix: self._RoutePrefix, | ||
beforeHandler: options.beforeHandler | ||
beforeHandler: options.beforeHandler, | ||
config: options.config | ||
}) | ||
@@ -300,2 +301,5 @@ } | ||
const config = opts.config || {} | ||
config.url = url | ||
const store = new Store( | ||
@@ -307,3 +311,4 @@ opts.schema, | ||
opts.contentTypeParser || _fastify._contentTypeParser, | ||
[] | ||
[], | ||
config | ||
) | ||
@@ -339,3 +344,3 @@ | ||
function Store (schema, handler, Reply, Request, contentTypeParser, preHandler) { | ||
function Store (schema, handler, Reply, Request, contentTypeParser, preHandler, config) { | ||
this.schema = schema | ||
@@ -347,2 +352,3 @@ this.handler = handler | ||
this.preHandler = preHandler | ||
this.config = config | ||
} | ||
@@ -349,0 +355,0 @@ |
@@ -136,2 +136,12 @@ /* eslint-disable no-useless-return */ | ||
Reply.prototype.redirect = function (code, url) { | ||
if (typeof code === 'string') { | ||
url = code | ||
code = 302 | ||
} | ||
this.res.writeHead(code, { Location: url }) | ||
this.res.end() | ||
} | ||
function wrapPumpCallback (reply) { | ||
@@ -138,0 +148,0 @@ return function pumpCallback (err) { |
{ | ||
"name": "fastify", | ||
"version": "0.25.3", | ||
"version": "0.26.0", | ||
"description": "Fast and low overhead web framework, for Node.js", | ||
@@ -59,5 +59,7 @@ "main": "fastify.js", | ||
"koa": "^2.3.0", | ||
"koa-router": "^7.2.1", | ||
"pino": "^4.7.0", | ||
"pre-commit": "^1.2.2", | ||
"request": "^2.81.0", | ||
"restify": "^5.0.1", | ||
"shot": "^3.4.2", | ||
@@ -64,0 +66,0 @@ "snazzy": "^7.0.0", |
@@ -31,8 +31,19 @@ <div align="center"> | ||
### Benchmarks | ||
- Hapi: 2200 req/sec | ||
- Restify: 6133 req/sec | ||
- Express: 8534 req/sec | ||
- Koa: 9640 req/sec | ||
- **Fastify: 21287 req/sec** | ||
__Machine:__ Intel Xeon E5-2686 v4 @ 2.30GHz (4 cores, 8 threads), 16GiB RAM (Amazon EC2 m4.xlarge) | ||
__Method:__: `autocannon -c 100 -d 5 -p 10 localhost:3000` * 2, taking the second average | ||
| Framework | Version | Router? | Requests/sec | | ||
| :----------------- | :------------------------- | :----------: | ------------: | | ||
| hapi | 16.5.0 | ✓ | 3,194 | | ||
| Express | 4.15.3 | ✓ | 9,418 | | ||
| Restify | 5.0.1 | ✓ | 12,014 | | ||
| take-five | 1.3.4 | ✓ | 18,658 | | ||
| Koa (`koa-router`) | 2.3.0 (`koa-router@7.2.1`) | ✓ | 19,650 | | ||
| Koa | 2.3.0 | ✗ | 21,349 | | ||
| **Fastify** | **0.25.2** | **✓** | **23,301** | | ||
| - | | | | | ||
| `http.Server` | 8.2.1 | ✗ | 33,435 | | ||
## Documentation | ||
@@ -75,4 +86,4 @@ * <a href="https://github.com/fastify/fastify/blob/master/docs/Getting-Started.md"><code><b>Getting Started</b></code></a> | ||
- [`fastify-leveldb`](https://github.com/fastify/fastify-leveldb) Plugin to share a common LevelDB connection across Fastify. | ||
- [`fastify-apollo`](https://github.com/coopnd/fastify-apollo) Run an [Apollo Server](https://github.com/apollographql/apollo-server) with Fastify. | ||
- [`fastify-accepts`](https://github.com/fastify/fastify-accepts) to have [accepts](https://www.npmjs.com/package/accepts) in your request object. | ||
- [`fastify-apollo`](https://github.com/coopnd/fastify-apollo) Run an [Apollo Server](https://github.com/apollographql/apollo-server) with Fastify. | ||
- [`fastify-accepts`](https://github.com/fastify/fastify-accepts) to have [accepts](https://www.npmjs.com/package/accepts) in your request object. | ||
- *More coming soon* | ||
@@ -79,0 +90,0 @@ |
@@ -100,2 +100,26 @@ 'use strict' | ||
test('Reply can redirect request', t => { | ||
t.plan(1) | ||
try { | ||
fastify.get('/redirect', function (req, reply) { | ||
reply.redirect('/') | ||
}) | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
}) | ||
test('Reply can redirect request (with status code)', t => { | ||
t.plan(1) | ||
try { | ||
fastify.get('/redirect-code', function (req, reply) { | ||
reply.redirect(301, '/') | ||
}) | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
}) | ||
fastify.listen(0, err => { | ||
@@ -148,2 +172,52 @@ t.error(err) | ||
}) | ||
test('redirect to `/` - 1', t => { | ||
t.plan(2) | ||
request({ | ||
method: 'GET', | ||
uri: 'http://localhost:' + fastify.server.address().port + '/redirect', | ||
followRedirect: false | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 302) | ||
}) | ||
}) | ||
test('redirect to `/` - 2', t => { | ||
t.plan(2) | ||
request({ | ||
method: 'GET', | ||
uri: 'http://localhost:' + fastify.server.address().port + '/redirect-code', | ||
followRedirect: false | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 301) | ||
}) | ||
}) | ||
test('redirect to `/` - 3', t => { | ||
t.plan(4) | ||
request({ | ||
method: 'GET', | ||
uri: 'http://localhost:' + fastify.server.address().port + '/redirect' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(response.headers['content-type'], 'text/plain') | ||
t.deepEqual(body, 'hello world!') | ||
}) | ||
}) | ||
test('redirect to `/` - 4', t => { | ||
t.plan(4) | ||
request({ | ||
method: 'GET', | ||
uri: 'http://localhost:' + fastify.server.address().port + '/redirect-code' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(response.headers['content-type'], 'text/plain') | ||
t.deepEqual(body, 'hello world!') | ||
}) | ||
}) | ||
}) |
219227
92
5668
117
29