اذهب إلى المحتوى
  • 0

كيفية عمل fastify-nextjs ؟

عبد النور محمد

السؤال

أنا أبحث عن نصيحة حول كيفية بدء fastify-cli مع fastify-nextjs

لقد حاولت إضافة رمز بسيط إلى المكان المقترح ، لكنه لا يعمل.

const path = require('path');
const AutoLoad = require('fastify-autoload');

module.exports = function (fastify, opts, next) {

  fastify.register(require('fastify-nextjs')).after(() => {
    fastify.next('/hello');
  });

  fastify.register(AutoLoad, {
    dir: path.join(__dirname, 'plugins'),
    options: Object.assign({}, opts),
  });

 
  fastify.register(AutoLoad, {
    dir: path.join(__dirname, 'services'),
    options: Object.assign({}, opts),
  });
  next();
};

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

نظرًا لأن Next.js يحتاج إلى بعض الوقت ليكون جاهزًا عند التشغيل الأول ، يجب أن تعلن عن مساراتك داخل callback ، بعد تسجيل المكون الإضافي. سيعرض المكون الإضافي واجهة برمجة التطبيقات التالية في Fastify والتي ستتعامل مع العرض نيابة عنك.

const fastify = require('fastify')()

fastify
  .register(require('fastify-nextjs'))
  .after(() => {
    fastify.next('/hello')
  })

fastify.listen(3000, err => {
  if (err) throw err
  console.log('Server listening on http://localhost:3000')
})

إذا كنت بحاجة إلى التعامل مع جزء العرض بنفسك ، فما عليك سوى تمرير callback إلى next

fastify.next('/hello', (app, req, reply) => {
  // your code
  // `app` is the Next instance
  app.render(req.raw, reply.raw, '/hello', req.query, {})
})

إذا كان لديك معالجة مسبقة مخصصة لطلبات _next / * ، فيمكنك منع هذا المعالجة باستخدام noServeAssets: الخاصية true لخيارات المكون الإضافي

fastify
  .register(require('fastify-nextjs'), {
    noServeAssets: true
  })
  .after(() => {
    fastify.next(`${process.env.BASE_PATH || ''}/_next/*`, (app, req, reply) => {
      // your code
      app.getRequestHandler()(req.raw, reply.raw).then(() => {
        reply.sent = true
      })
    })
  })

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

أولاً قم بتثبيت fastify-cli 

npm i fastify-cli

يمكنك ايضاً تثبيتها عالمياً بإضافة g-

ثم قم بإضافة السطور التالية الى package.json

{
  "scripts": {
    "start": "fastify start server.js"
  }
}

وقم بإنشاء ملف الخادم server.js وإضافة الكود الآتي

// server.js
'use strict'

module.exports = async function (fastify, opts) {
  fastify.get('/', async (request, reply) => {
    return { hello: 'world' }
  })
}

ومن ثم قم بتشغيل الخادم ب:

npm start

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...