fastify
Advanced tools
Comparing version 0.25.1 to 0.25.2
@@ -5,8 +5,10 @@ <h1 align="center">Fastify</h1> | ||
Fastify provides out of the box an asynchronous [middleware engine](https://github.com/fastify/middie) compatible with Express and Restify middlewares. | ||
Fastify provides out of the box an asynchronous [middleware engine](https://github.com/fastify/middie) compatible with [Express](https://expressjs.com/) and [Restify](http://restify.com/) middlewares. | ||
This does not support the full syntax `middleware(err, req, res, next)`, because error handling is done inside Fastify. | ||
*If you need a visual feedback to understand when the middlewares are executed take a look to the [lifecycle](https://github.com/fastify/fastify/blob/master/docs/Lifecycle.md) page.* | ||
If you are using Middlewares that bundles differets small middlewares, such as *helmet*, we recommend to use the single modules to get better performances. | ||
Fastify middlewares don't support the full syntax `middleware(err, req, res, next)`, because error handling is done inside Fastify. | ||
Also, if you are using Middlewares that bundles different small middlewares, such as [*helmet*](https://helmetjs.github.io/), we recommend to use the single modules to get better performances. | ||
```js | ||
@@ -21,19 +23,31 @@ fastify.use(require('cors')()) | ||
``` | ||
*If you need a visual feedback to understand when the middlewares are executed take a look to the [lifecycle](https://github.com/fastify/fastify/blob/master/docs/Lifecycle.md) page.* | ||
or, in the specific case of *helmet*, you can use the [*fastify-helmet*](https://github.com/fastify/fastify-helmet) [plugin](Plugins.md), which is an optimized helmet integration for fastify: | ||
```js | ||
const fastify = require('fastify')() | ||
const helmet = require('fastify-helmet') | ||
fastify.register(helmet) | ||
``` | ||
<a name="restrict-usage"></a> | ||
#### Restrict middleware execution to a certain path(s) | ||
If you need to run a middleware only under certains path(s), just pass the path as first parameter to `use` and you are done! | ||
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! | ||
*Note that this does not support routes with parameters, (eg: `/user/:id/comments`) and wildcard is not supported in multiple paths.* | ||
```js | ||
const serveStatic = require('serve-static') | ||
// Single path | ||
middie.use('/public', staticFiles('/assets')) | ||
fastify.use('/css', serveStatic('/assets')) | ||
// Wildcard path | ||
middie.use('/public/*', staticFiles('/assets')) | ||
fastify.use('/css/*', serveStatic('/assets')) | ||
// Multiple paths | ||
middie.use(['/public', '/dist'], staticFiles('/assets')) | ||
fastify.use(['/css', '/js'], serveStatic('/assets')) | ||
``` |
@@ -47,3 +47,3 @@ <h1 align="center">Fastify</h1> | ||
fastify.ready(() => | ||
fastify.ready(() => { | ||
for (var route of fastify) { | ||
@@ -50,0 +50,0 @@ console.log(route) |
@@ -220,4 +220,4 @@ 'use strict' | ||
function buildRoutePrefix (r, opts) { | ||
function _RoutePrefix () {} | ||
const R = new _RoutePrefix() | ||
const _RoutePrefix = Object.create(opts) | ||
const R = _RoutePrefix | ||
R.prefix = r.prefix | ||
@@ -283,2 +283,4 @@ if (typeof opts.prefix === 'string') { | ||
function route (opts) { | ||
const _fastify = this | ||
if (supportedMethods.indexOf(opts.method) === -1) { | ||
@@ -292,5 +294,7 @@ throw new Error(`${opts.method} method is not supported!`) | ||
this.after((notHandledErr, done) => { | ||
_fastify._RoutePrefix = opts.RoutePrefix || _fastify._RoutePrefix | ||
_fastify.after((notHandledErr, done) => { | ||
const path = opts.url || opts.path | ||
const prefix = (opts.RoutePrefix || this._RoutePrefix).prefix | ||
const prefix = _fastify._RoutePrefix.prefix | ||
const url = prefix + (path === '/' && prefix.length > 0 ? '' : path) | ||
@@ -301,5 +305,5 @@ | ||
opts.handler, | ||
opts.Reply || this._Reply, | ||
opts.Request || this._Request, | ||
opts.contentTypeParser || this._contentTypeParser, | ||
opts.Reply || _fastify._Reply, | ||
opts.Request || _fastify._Request, | ||
opts.contentTypeParser || _fastify._contentTypeParser, | ||
[] | ||
@@ -310,3 +314,3 @@ ) | ||
store.preHandler.push.apply(store.preHandler, (opts.preHandler || this._hooks.preHandler)) | ||
store.preHandler.push.apply(store.preHandler, (opts.preHandler || _fastify._hooks.preHandler)) | ||
if (opts.beforeHandler) { | ||
@@ -334,3 +338,3 @@ opts.beforeHandler = Array.isArray(opts.beforeHandler) ? opts.beforeHandler : [opts.beforeHandler] | ||
// chainable api | ||
return fastify | ||
return _fastify | ||
} | ||
@@ -337,0 +341,0 @@ |
{ | ||
"name": "fastify", | ||
"version": "0.25.1", | ||
"version": "0.25.2", | ||
"description": "Fast and low overhead web framework, for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "fastify.js", |
@@ -26,3 +26,3 @@ <div align="center"> | ||
- **Extendible:** Fastify is fully extensible via its hooks, plugins and decorators. | ||
- **Schema based:** even if it is not mandatory we recommend to use [JSON Schema](http://json-schema.org/) to validate you routes and serialize your outputs, internally Fastify compiles the schema in an highly performant function. | ||
- **Schema based:** even if it is not mandatory we recommend to use [JSON Schema](http://json-schema.org/) to validate your routes and serialize your outputs, internally Fastify compiles the schema in an highly performant function. | ||
- **Logging:** logs are extremely important but are costly; we chose the best logger to almost remove this cost, [Pino](https://github.com/pinojs/pino)! | ||
@@ -72,3 +72,6 @@ - **Developer friendly:** the framework is built to be very expressive and help the developer in his daily use, without sacrificing performance and security. | ||
- [`fastify-helmet`](https://github.com/fastify/fastify-helmet) Important security headers for Fastify | ||
- [`fastify-multipart`](https://github.com/fastify/fastify-multipart) Support for multipart requests | ||
- [`fastify-auth`](https://github.com/fastify/fastify-auth) Run multiple auth functions in Fastify | ||
- [`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. | ||
- *More coming soon* | ||
@@ -75,0 +78,0 @@ |
@@ -80,2 +80,33 @@ 'use strict' | ||
test('Prefix options should add a prefix for all the chained routes inside a register / 3', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify.register(function (fastify, opts, next) { | ||
fastify | ||
.get('/first', (req, reply) => { | ||
reply.send({ route: '/v1/first' }) | ||
}) | ||
.get('/second', (req, reply) => { | ||
reply.send({ route: '/v1/second' }) | ||
}) | ||
next() | ||
}, { prefix: '/v1' }) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/v1/first' | ||
}, res => { | ||
t.same(JSON.parse(res.payload), { route: '/v1/first' }) | ||
}) | ||
fastify.inject({ | ||
method: 'GET', | ||
url: '/v1/second' | ||
}, res => { | ||
t.same(JSON.parse(res.payload), { route: '/v1/second' }) | ||
}) | ||
}) | ||
test('Prefix should support parameters as well', t => { | ||
@@ -82,0 +113,0 @@ t.plan(1) |
200647
5488
105