Task Detail

  • Activity on Fri May 25 2018 12:12:03 GMT+0000 (UTC) 1. In the server.js, write a logic to register all the plugin codes to the server as we would write them directly in server.js 2. Get all the paths all the way to index.js in plugins folder: const pluginPaths = await dir.promiseFiles(path.join(__dirname, '..', 'plugins')) //node-dir module do the job. 3. Because all the index.js under plugins have module.exports, so we can loop through and require the files (index.js) to the server.js: const urlPaths = require(pluginPaths[i]); 4. After require the files, we now can register the routes into the server.js - just like we would write these routes inside server.js await fastify.register(urlPaths); 5. The whole logic: const pluginPaths = await dir.promiseFiles(path.join(__dirname, '..', 'plugins')) console.log('pluginPaths '+pluginPaths); for(let i=0; i < pluginPaths.length; i++){ log.trace('registering plugin at %s', pluginPaths[i]) //register the whole fastify.get(....) into fastify. const urlPaths = require(pluginPaths[i]); //console.log('url paths: '+urlPaths); await fastify.register(urlPaths); }

    10/26/2024 14:32:55
  • Activity on Fri May 25 2018 11:59:40 GMT+0000 (UTC) 1. Created a plugins folder. 2. Created a first home folder for the first plugin/controller (index.js) 3. Keep in mind modularity style during creating home/index.js --> This file shouldn't be interfere with anything for now when the server start up. It has its own code until the server.js has the logic to catch it. 'use strict' async function home (fastify, options) { fastify.get('/', async (req, reply) => { //return { hello: '/need2redirect to /home' } reply.view('/view/home/index', {text:'text'}); }) // fastify.get('/home', async(req, reply)=>{ // reply.view('/view/home/index', {text:'text'}); // }) } module.exports=home

    10/26/2024 14:32:44