For us developers is an unwritten law to educate ourselves and keep track of new tools and technologies. It’s essential to continuously learn and make personal progress. You don’t want to be that guy who’s stuck in the 90’s with tools that are outdated since years!
The hapi framework has an awesome ecosystem and great developers that spend a lot of their spare time contributing on hapi and associated plugins. Now is the best time to reflect and decide on what to improve in 2017. This article comprises the (for us) important aspects to advance in your personal development when working with hapi or generally Node.js.
hapi Series Overview
- Get Your Server Up and Running
- v17 Upgrade Guide (Your Move to async/await)
- Become a Better Developer in 2018
- Become a Better Developer in 2017
- What You’ll Build
- Prepare Your Project: Stack & Structure
- Environment Variables and Storing Secrets
- Set Up MongoDB and Connect With Mongoose
- Sending Emails in Node.js
- Load the User’s Profile Picture From Gravatar Using Virtuals in Mongoose
- Implement a User Profile Editing Screen
- Generate a Username in Mongoose Middleware
- Displaying Seasons and Episodes for TV Shows with Mongoose Relationship Population
- Implementing Pagination for Movies
- Implement a Watchlist
- Create a Full Text Search with MongoDB
- Create a REST API with JSON Endpoints
- Update Mongoose Models for JSON Responses
- API Pagination for TV Shows
- Customize API Endpoints with Query Parameters
- Always Throw and Handle API Validation Errors
- Advanced API Validation With Custom Errors
- Create an API Documentation with Swagger
- Customize Your Swagger API Documentation URL
- Describe Endpoint Details in Your Swagger API Documentation
- 10 Tips on API Testing With Postman
- JWT Authentication in Swagger API Documentation
- API Versioning with Request Headers
- API Login With Username and Password to Generate a JWT
- JWT Authentication and Private API Endpoints
- Refresh Tokens With JWT Authentication
- Create a JWT Utility
- JWT Refresh Token for Multiple Devices
- Check Refresh Token in Authentication Strategy
- Rate Limit Your Refresh Token API Endpoint
- How to Revoke a JWT
- Invalidate JWTs With Blacklists
- JWT Logout (Part 1/2)
- JWT “Immediate” Logout (Part 2/2)
- A Better Place to Invalidate Tokens
- How to Switch the JWT Signing Algorithm
- Roll Your Own Refresh Token Authentication Scheme
- JWT Claims 101
- Use JWT With Asymmetric Signatures (RS256 & Co.)
- Encrypt the JWT Payload (The Simple Way)
- Increase JWT Security Beyond the Signature
- Unsigned JSON Web Tokens (Unsecured JWS)
- JWK and JWKS Overview
- Provide a JWKS API Endpoint
- Create a JWK from a Shared Secret
- JWT Verification via JWKS API Endpoint
- What is JOSE in JWT
- Encrypt a JWT (the JWE Way)
- Authenticate Encrypted JWTs (JWE)
- Encrypted and Signed JWT (Nested JWT)
- Bringing Back JWT Decoding and Authentication
- Bringing Back JWT Claims in the JWT Payload
- Basic Authentication With Username and Password
- Authentication and Remember Me Using Cookies
- How to Set a Default Authentication Strategy
- Define Multiple Authentication Strategies for a Route
- Restrict User Access With Scopes
- Show „Insufficient Scope“ View for Routes With Restricted Access
- Access Restriction With Dynamic and Advanced Scopes
- hapi - How to Fix „unknown authentication strategy“
- Authenticate with GitHub And Remember the Login
- Authenticate with GitLab And Remember the User
- How to Combine Bell With Another Authentication Strategy
- Custom OAuth Bell Strategy to Connect With any Server
- Redirect to Previous Page After Login
- How to Implement a Complete Sign Up Flow With Email and Password
- How to Implement a Complete Login Flow
- Implement a Password-Reset Flow
- Views in hapi 9 (and above)
- How to Render and Reply Views
- How to Reply and Render Pug Views (Using Pug 2.0)
- How to Create a Dynamic Handlebars Layout Template
- Create and Use Handlebars Partial Views
- Create and Use Custom Handlebars Helpers
- Specify a Different Handlebars Layout for a Specific View
- How to Create Jade-Like Layout Blocks in Handlebars
- Use Vue.js Mustache Tags in Handlebars Templates
- How to Use Multiple Handlebars Layouts
- How to Access and Handle Request Payload
- Access Request Headers
- How to Manage Cookies and HTTP States Across Requests
- Detect and Get the Client IP Address
- How to Upload Files
- Quick Access to Logged In User in Route Handlers
- How to Fix “handler method did not return a value, a promise, or throw an error”
- How to Fix “X must return an error, a takeover response, or a continue signal”
- Query Parameter Validation With Joi
- Path Parameter Validation With Joi
- Request Payload Validation With Joi
- Validate Query and Path Parameters, Payload and Headers All at Once on Your Routes
- Validate Request Headers With Joi
- Reply Custom View for Failed Validations
- Handle Failed Validations and Show Errors Details at Inputs
- How to Fix AssertionError, Cannot validate HEAD or GET requests
Use ES2015
The hapi framework itself and plugins filed under the hapijs GitHub organization are already updated to use ES2015 and you should do that as well. With the release of Node.js v4 the ES2015 support was at 57% and you needed transpilers (like Babel) or polyfills to fully benefit from features that are not yet available. With the release of Node.js v6, the ES2015 support grew to 99% and there’s no excuse to not use the newly available capabilities in your development with hapi.
More details on ES2015 support in Node.js are available at node.green.
Use Latest Node.js LTS
The hapi community is blazing fast in making the modules compatible with the latest Node.js LTS. Weeks before v6 was declared LTS, all hapi projects were v6-ready and you could leverage the benefits immediately at release.
Make sure your own plugins and code bases are ready for upcoming Node.js releases to allow yourself and other developers to advance and use new technologies.
Support (And Use) Promises
Every developer should be open to new coding paradigms and the Node.js community is split-head between callback fanatics and promise aficionados and async/await early birds. There’s no right or wrong, use what you prefer!
As a module or plugin developer, allow your users to benefit from promises as well. It’s some kind of overhead to support promises additionally to callbacks, so play the long game and it will definitely pay off. Read more about callback and promise support in Node.js modules.
Due to hapi’s powerful plugin system, there’s no immediate benefit of supporting promises and callbacks. However, don’t wrap your mind just around callbacks, be open to support promises when they make sense.
Use Plugins to Develop Your App
As a hapi developer, you know that the plugin system is the core of all good! It’s a best-practice to use plugins extending the framework to add your desired functionality.
Unraveling your project’s code base and put functionality into separated plugins will take some time. Of course it’s nasty work, because you’re in your comfort zone of just using your application as it is. Give yourself the time to think about your project and the possibilities to isolate functionality into a plugin.
Do you use utility functionality over and over in your route handlers? Why not create a plugin that decorates the request
interface and provides the functionality in your route handlers.
Review your code base, structure and architecture. Plan to use plugins to provide the individual functionality and in the long run: benefit from separated concerns and a much better setup. And you can easily reuse the plugins in other projects, too!
Don’t Reinvent the Wheel
I know that we as developers tend to solve problems on our own. We don’t like to rely on code that solves a problem but isn’t formatted the way we want it to be. We don’t like to use the implementation of others because there’s a missing option that is required in our use case. We need to write it ourselves to get it right!
In those situations, remind yourself to take a deep breath. Is it worth to spend the development time and effort to reimplement the functionality again so that it matches our preferred formatting and options? In almost every scenario, we should spend our time otherwise and not solving already solved problems. Extend the existing solution if necessary. Else: take it and win.
Use a Coding Style
Every developer has its own coding style and a preferred style guide in mind. Especially when working with a team, you should agree on a coding style like the hapi coding conventions or the JavaScriptStandard Style or whatever style you and your team prefer. Put some time into researching available styles or just agree on a custom one. Tools like EditorConfig help you to create and maintain a coding style with support for a lot IDEs.
Use an Error Tracking Service/System
At Future Studio, we’ve added error tracking just in September 2016 and fell immediately in love with it. We chose Sentry and are very happy with their platform and service. It’s a great tool! And besides all the local testing of your application, there are various scenarios that you can’t test during development.
For example, with the launch of the Future Studio University we’ve added the Braintree integration for payment handling. Know what? During development, everything was smooth and we fixed all bugs and tested various scenarios. As soon as the first student enrolled, we got a webhook notification from Braintree and a typo just crashed the flow to send a welcome email. It was just a little typo … and we wouldn’t have known about that issue without an error tracking platform.
We highly recommend to integrate such a system sooner than later. It’s definitely worth the effort! Just to name a few: Sentry, Rollbar, Bugsnag, Raygun, Airbrake. There are many more, just decide for one. You can’t go wrong.
Be Open to Give and Not Just Take: Engage in the Community
As a hapi developer, you’re facing and fighting many personal battles with the framework and plugins. You gain a lot expertise in specific functionalities of hapi and its ecosystem. Share your knowledge with the community. Take some minutes every day to answer issues on GitHub about hapi and plugins.
It will help us grow as a group of developers and keeps the feeling of an inclusive team. Share your tips and tricks in your personal blog or on twitter. The @hapijs user on twitter is ready to share your thoughts with all the followers. Take a step forward.
Solve the Most Simplest Problem First
It’s easier said than done. Usually you think in large problems and want to solve it all at once. Or even better, just come up with the best and sexiest solution at your first try. You know, that’s an illusion and there’s always a better way to solve the problem.
Especially with complex problems, it’s hard to wrap your mind around the actual problem and to keep all the details in mind that you need to keep an eye on. How to solve that? Break your large problem into multiple smaller ones. In such small problems, solve the little nasty details. Ultimately, compose all the small pieces to solve the large problem. Of course you need to glue your small solutions together to result in a solution for the complex problem. But that’s the easy road to success.
Become a Better hapi Developer in 2017
Now is the best time to reflect yourself and plan for 2017. Take actions on the points from above where you identify the most. Improve on those and become the developer you want to be.
Just to remind you of possible actions to take:
- Use ES2015
- Use Latest Node.js LTS
- Support (And Use) Promises
- Use Plugins to Develop Your App
- Don’t Reinvent the Wheel
- Use a Coding Style
- Use an Error Tracking Service/System
- Be Open to Give and Not Just Take: Engage in the Community
- Solve the Most Simplest Problem First
Give yourself accountability by sharing your actions in the comments below or tweet us @futurestud_io. Do you have further aspects and tools for 2017 where you what you want to improve? Please share it with us :)
Make it rock in 2017!