Smarter starters and better error handling, thanks to Fastify 4!
All of our Glitch Node.js starter projects use the efficient and flexible server framework Fastify. The Fastify team recently released a major update–version 4 includes a number of improvements, but you will need to make a few changes to any existing remixes you created on Glitch before we updated the starters to use it. Read on to learn more!
Version 4 changes #
You can read all about the changes in Fastify 4 in the announcement blog post–here are a few highlights:
- Callback and promise confusion has been reduced by requiring calls to reply to be part of a return statement.
- Error handling is now encapsulated to improve consistency.
- Type providers are now included, allowing your apps to infer types from JSON Schema.
- The listen signature has been simplified, with a view to improved contributor maintenance experience.
- The minimum Node version now supported is 14. Kudos to the Fastify maintainers and community for consistently developing a framework that affords Glitch Node.js apps such great performance!
Updating your existing apps #
When you edit a Node.js app in Glitch, you can manage your npm dependencies in the package.json file using the dropdown list that shows you any packages that have updates available. If you have a remix of Hello Node, Hello SQLite, or the blank / minimal versions of either of these starter projects, you’ll see the Fastify 4 update in the dropdown list–but you’ll need to make a few additional changes to get your app working with the new version.
Fastify 4 requires at least Node version 14, and there are some packages the starters use that have been deprecated and replaced–there are also a couple of minor server syntax changes.
In your project, open the Logs so that you see the progress unfold. Don’t worry when you see the little sweary face in your app Status at the bottom of the Glitch editor as you carry out the updates–it’s all part of the fun and they’ll turn happy again by the time you’re done!
Check out the help doc for detailed steps, here’s the TLDR; on what you’ll need to change:
- Update the node version in your package JSON to 14
- Update the fastify package to 4+
- Remove any of the deprecated packages: fastify-static, fastify-formbody, point-of-view
- Add any of these that you still plan on using with their replacements: @fastify/static, @fastify/view, @fastify/formbody
- Update references to these in your server JS code
- Update your server JS code listen function to pass the host and port values inside an object, like this: fastify.listen({port:process.env.PORT, host:'0.0.0.0'}...
- Include calls to reply in a return statement
Give Glitch a little time to update and rebuild your project between steps and hopefully it’ll work the way you expect! If you have any issues take a look in the Logs, and turn on detailed logging in your server JS file if necessary.
Check out the updated starters to see the new syntax in action:
- Hello Node
- Blank Node
- Hello SQLite
- Blank SQLite Are you building anything new with the updated Fastify capabilities? Share it on the community forum!
*Those two beautiful cheetah photographs at the top of the post were taken and shared with the commons by William Warby and James Temple 🐆