hapi-routes-loader
Coverage 100% A custom route loader for hapijs.
This module aims you write routes into separate files and load those without to require each file. Also, this module add a prefix with the file name in each declared path.
Compatibility
hapi-routes-loader version | hapi.js | node version |
---|---|---|
v0.3.0 |
>=18 @hapi/hapi |
>=11 |
v0.2.0 |
=13 hapi |
>=0.10 |
Installation
$ [sudo] npm install hapi-routes-loader --save
Usage
Following example attaches hapi-routes-loader to a simple node.js app (you can see test for more examples)
Directory structure:
├── example/
│ ├── public /
│ │ ├── assets/
│ │ │ │ ├── cs/
│ │ │ │ ├── js/
│ │ │ │ ├── img/
│ ├── app /
│ │ ├── routes/
│ │ │ ├── admin.js/
│ │ │ ├── user.js/
│ │ │ ├── account.js/
│ │ │ ├── assets.js/
│ │ ├── controllers/
│ │ ├── models/
│ │ ├── helpers/
. . . . . . .
. . . . . . .
. . . . . . .
│ ├── server.js
app/routes/user.js
'use strict'; moduleexports = method: 'GET' path: 'index' config: auth: false { ; } method: 'GET' path: 'dashboard' config: auth: false { ; };
app/routes/admin.js
//You can see more examples into test/routes folder
'use strict'; moduleexports = method: 'GET' path: 'index' config: auth: false { ; } method: 'GET' path: 'dashboard' config: auth: false { ; };
app/routes/account.js
//You can see more examples into test/routes folder
'use strict'; moduleexports = method: 'GET' path: 'index' config: auth: false { ; } method: 'GET' path: 'dashboard' config: auth: false { ; };
server.js
'use strict'; const Hapi = ;const Inert = ;const HapiRoutesLoader = ; const init = async { const server = Hapi; //register HapiRoutesLoader as a hapi's plugin await server; await serverstart; console;}; process; ;
execute
node index.js
Open your browser and visit http://localhost:8000 and test your aplication with every registered routes in each file. For example:
http://localhost:8000/account/index http://localhost:8000/account/dashboard
http://localhost:8000/admin/index http://localhost:8000/admin/dashboard
http://localhost:8000/user/index http://localhost:8000/user/dashboard
Testing
npm run test
Support
If you need help using hapi-routes-loader, or have found a bug, please create an issue on the GitHub repo.
License
MIT Licence