Fastify هو إطار عمل ويب عالي الكفاءة ومنخفض الحمل الإضافي، يركز بشكل كبير على تقديم تجربة مطور مريحة وسرعة معالجة فائقة.
البدء السريع
المقدمة والتوثيق
Fastify يقدم أداء استثنائيا بفضل التجميع القائم على المخطط والبنية المكونة من إضافات قوية.
- Fastify Source Code (github.com)
- Fastify Documentation (fastify.dev)
تطبيق مرحبًا بالعالم (Hello World)
const fastify = require('fastify')()
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen(3000, err => {
if (err) throw err
const port = fastify.server.address().port
console.log(`server listening on ${port}`)
})
التوجيه (Routing)
تعريف المسار الشامل
fastify.route({
method: 'GET',
url: '/',
schema: { ··· },
handler: (req, reply) => { ··· },
beforeHandler: (req, reply, done) => { ··· }
})
الدوال المختصرة (Shorthand)
fastify.get(path, [options], handler)
fastify.head(path, [options], handler)
fastify.post(path, [options], handler)
fastify.put(path, [options], handler)
fastify.delete(path, [options], handler)
fastify.options(path, [options], handler)
fastify.patch(path, [options], handler)
معالجات الدوال غير المتزامنة (Async / Await)
fastify.get('/', options, async (req, reply) => {
return data
// or
reply.send(data)
})
مع المعالجات غير المتزامنة، يمكنك إرجاع البيانات مباشرة أو استدعاء reply.send(data).
الطلب والرد (Request & Reply)
كائن الطلب (Request)
request.query
request.body
request.params
request.headers
request.req // Node.js core incoming request
request.log.info('hello')
See: Request.
كائن الرد (Reply)
ترويسات الاستجابة
reply.code(404)
reply.header('Content-Type', 'text/html')
reply.type('text/html')
إعادة التوجيه (Redirects)
reply.redirect('/foo')
reply.redirect(302, '/foo')
إرسال البيانات
reply.send(payload)
reply.sent // → true|false
See: Reply.
التحقق من صحة مخطط JSON
تعريف المخطط (Schema)
const schema = {
querystring: {
name: { type: 'string' },
excitement: { type: 'integer' }
},
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}
تمرير المخطط إلى المسار
fastify.get('/', { schema }, (req, reply) => {
···
})
كائن خيارات المسار
fastify.route({
method: 'GET',
url: '/',
schema,
handler: (req, reply) => { ··· }
})
يتيح تعريف مخطط JSON تسريع التحقق التسلسلي الفائق والتحويل التلقائي للبيانات.
See: Validation and Serialization.
نظام الإضافات (Plugins)
تسجيل دالة كمكون إضافي
fastify.register(
require('./route'),
err => { if (err) throw err }
)
route.js
module.exports = (fastify, options, next) => {
fastify.get('/', (req, reply) => { ··· })
next()
}
See: Plugins.
تسجيل إضافات متعددة
fastify.register([
require('./another-route'),
require('./yet-another-route')
], opts, (err) => {
if (err) throw err
})
بادئات المسارات (Route Prefixes)
fastify.register(
require('./route'),
{ prefix: '/v1' }
)
ستتم إضافة البادئة تلقائيًا إلى جميع المسارات المحددة داخل سياق هذه الإضافة.
مكتبة fastify-plugin
const fp = require('fastify-plugin')
module.exports = fp((fastify, opts, next) => {
fastify.decorate('utility', () => {})
next()
}, '0.x')
تسمح بتجاوز عزل النطاق ومشاركة الزخارف (decorators) عبر النطاقات الأبوية.
البرمجيات الوسيطة والقوالب
دمج البرمجيات الوسيطة التقليدية
fastify.use(require('cors')())
fastify.use(require('dns-prefetch-control')())
fastify.use(require('frameguard')())
fastify.use(require('hide-powered-by')())
fastify.use(require('hsts')())
fastify.use(require('ienoopen')())
fastify.use(require('x-xss-protection')())