Task Detail

  • Activity on Mon Jun 04 2018 11:58:26 GMT+0000 (UTC) 1. Create a plugin to serve common router for now. 2. I will need to figure out how to DRY for the logger and the common modules for all plugins. 3. Remember, register plugins in server.js is a little different then add routes in each plugin. 3a. Register plugins in server, each plugin is exported by itself, so fastify.register(plugins) take in one at the time. 3b. Add route in plugin, each file in route folder might have more than one routes, so require(routeDirs[i]) taking in array of routes. Therefore I have to fastify.route(require(routeDirs[i])[i]) to add one route for each loop. 'use strict' const log=require('../../lib/logger.js')(); const fastify=require('fastify')({logger:log}); const path=require('path'); const dir = require('node-dir'); async function pluginOne(fastify, option){ const routeDirs=await dir.promiseFiles(path.join(__dirname, '../..', 'routes')); let routes=[]; for(let i=0; i<routeDirs.length; i++){ log.trace('plugin-one register route at %s', routeDirs[i]); routes=routes.concat(require(routeDirs[i])); log.trace('routes for plugin-one', routes[i]); fastify.route(routes[i]); } } module.exports=pluginOne

    10/26/2024 14:25:16