Task Detail

  • Activity on Tue Apr 10 2018 20:35:23 GMT+0000 (UTC) 1. Inserting the routes into the server. Remember the routes included their method, path, and handler. /*** insert routes into the server **********/ function insertRoutes2Server(routes){ return new Promise((resolve,reject)=>{ server.route(routes) resolve() }) } /*** end insert routes into the server *****/ 2. In init() function: module.exports={ init: async function(){ try{ var filePathArray = await getRouteFilePaths(); console.log(filePathArray); var routes = await getAndConcatRoutes(filePathArray); await insertRoutes2Server(routes); await startServer(); } catch(err){ console.log(err); } } } 3. I ran a test localhost:8000/home, and it was working fine Activity on Tue Apr 10 2018 20:30:58 GMT+0000 (UTC) 1. I needed to get the routes out of the filePathArray, so I can insert those routes into the server. 2. I created a getAndConcatRoutes(filePathArray) function and take in filePathArray. 3. I used 'require' module to get the routes out of each sub directory in controller dir. 4. I then concat all the routes into routes array. 5. After all, I resolve the routes array, so I can insert them into the sever later. /*** get each route and concat them in an array *****/ function getAndConcatRoutes(filePathArray){ return new Promise((resolve, reject)=>{ let routes = []; for(let i=0; i<filePathArray.length; i++){ const routesInControllerSubDir = require(filePathArray[i]); routes = routes.concat(routesInControllerSubDir); if(i === filePathArray.length - 1){ console.log(routes); resolve (routes); } } }); } /*** get each route, and concat them in an array *****/ 6. in init() function: module.exports={ init: async function(){ try{ var filePathArray = await getRouteFilePaths(); console.log(filePathArray); var routes = await getAndConcatRoutes(filePathArray); console.log(routes); await startServer(); } catch(err){ console.log(err); } } } 7. console.log(routes) print out the routes in an array fine. Activity on Tue Apr 10 2018 20:21:51 GMT+0000 (UTC) 1. I created getRouteFilePaths() function to get all the paths of all the index.js in controller/sub-dir 2. The function is a Promise function, so I can return the paths to get the routes later. /**** get controller folder paths ****/ function getRouteFilePaths(){ return new Promise((resolve, reject) =>{ const controllerDirPath=path.join(__dirname, '..', 'controller'); dir.files(controllerDirPath, (err, filePathArray)=>{ if(err){ console.log(err); }else{ console.log(filePathArray); resolve (filePathArray); } }) }) } /*** end get controller folder paths ****/ 3. const path = require('path'); - this module helped me get the path to the controller/sub-dir. 4. const dir = require('node-dir'); - this module helped me get the /index.js at the end of each sub-dir 5. I added the function to the init() function as an await, so it can run before other functions. 6. console.log(filePathArray) - to printout the result to make sure this is working. module.exports={ init: async function(){ try{ var filePathArray = await getRouteFilePaths(); console.log(filePathArray); await startServer(); } catch(err){ console.log(err); } } } 7. The function was working fine. Activity on Tue Apr 10 2018 20:12:29 GMT+0000 (UTC) 1. Before getting the /home route into the server, I need to make sure the server.js get kick off by /index.js 2. Inside server.js file, I exported init function, so the /index.js can require the server.js file. 3. I moved startServer() into init function, so it can kick of the startServer() 3. I ran a test, and it was working fine. module.exports={ init: async function(){ try{ await startServer(); } catch(err){ console.log(err); } } } Activity on Tue Apr 10 2018 20:06:34 GMT+0000 (UTC) 1. I wanted to move all the routes to separate folders under controller folder, so they can be easier to manage. 2. I created controller/home/index.js. 3. In index.js, I created a simple function and route 'use strict' function handleHome(req, h){ return 'hello, world'; } module.exports={ method:'GET', path:'/home', handler: handleHome } 4. Now I have to figure out how to insert this route which include method, path, and handler into the server. Activity on Tue Apr 10 2018 20:00:43 GMT+0000 (UTC) 1. Since the basic route was working fine, I moved the server.js file into /lib folder. 2. Created /index.js file to kick off the server.js file 'use strict'; require('require-self-ref'); (async function main(){ await require('~/lib/server').init(); })() .catch((err)=>{ throw err; }); 3. require('require-self-ref'); - this module allows one to refer to the project root with ~, really helpful with super nested files, and it would only need to be required one time through out the whole project Activity on Tue Apr 10 2018 19:56:20 GMT+0000 (UTC) 1. Create and test the server to make sure everything working from the beginning. const Hapi = require('hapi'); /*** create server ********/ const server=Hapi.server({ host:'localhost', port:8000 }); /*** end create server ****/ /*** testing route ****/ server.route({ method: 'GET', path: '/hello', handler: (request, h) => { return 'hello there'; } }); /*** end testing route ****/ /**** start server ***/ async function startServer(){ try{ await server.start(); } catch(err){ console.log(err); process.exit(1); } console.log('Server running at: ', server.info.uri) }; /** end start server ****/ start(); 2. http://localhost:8000/hello was working fine

    10/31/2024 14:05:53