Express is one of the most widely used web server libraries in Node.js, and with good reason. It manages to find a balance between feature set and simplicity that few libraries and frameworks can match.
But the balance that is strikes is sometimes overshadowed by it’s lack of guidance when it comes to architecture, clean code, and keeping everything running smoothly for years to come.
This guide starts with the assumption that you know how to write basic Express apps already, but are looking to clean up your routes, modularize your code and ultimately build a simple yet robust architecture that will scale with your team, your feature set and your active users.
Part 1: Cleaning Your Express.js Code
A cursory search for any blog post or StackOverflow answer on Express.js routes will show you a myriad of examples with dozens if not hundreds of lines of code in an Express.js route method. But anyone that has dealt with Express for more than just sample applications, and has to actively maintain the code in these routes, knows that this is problematic.
Too much code in a route method makes the method hard to understand. It introduces potential bugs with multiple exit points, and it typically means the route method is full of deeply nested callback structures.
With proper use of Express routes, middleware, Node.js modules and a few other tricks, however, this problem can be averted. Your Express.js code can be clean, maintainable and limited to only a few lines of code!
Express v4 Routes And Route Organization
Express Route Params
Express Error Handling
Organizing Business Logic in Express Apps
Express Middleware Basics
Express Session Basics
Express Session Storage w/ MongoDB
Cleaning Up Express Routes w/ Middleware
Part 2: A Real-World Service
It’s good to see example code, as shown above, where the principles and patterns can be the focus. But when it comes time to take the ideal code and apply to the real-world, we are often left standing around wondering how to make it work.
get up to speed with MongoDB and MongooseJS.
With the Express architecture screencasts, you’ll see the principles best practices taken from above, and applied to an actual service that is still in use with WatchMeCode.
Architecting Express – The Basics (FREE!)
Architecting Express – Amazon S3 and AWS SDK
Architecting Express – A File Model w/ MongoDB
Architecting Express – Initializers
Architecting Express – Filling In The Files
Architecting Express – Get File By Name
Architecting Express – Route and Config Cleanup
Architecting Express – Proxying HEAD Requests
Architecting Express – Fixing A Unique Index
Architecting Express – Updating The File List
Part 3: Growing The Architecture
Building a small application is relatively easy. You keep all your code in the project, directly, and you typically have a single running process that handles everything.
When you find yourself needing to grow your architecture beyond a simple, single process, though, what do you do? How do you handle growth in users and use, while dealing with potentially long-running or error-prone background processes?
get up to speed with RabbitMQ and messaging patterns.
In this part of the guide, you’ll learn the secrets of growing an architecture that can handle these needs gracefully: running code in separate processes, and communicating with message queues.
Analytics and Tracking
Analytics and Tracking w/ Keen.io
Analytics Background Process w/ RabbitMQ
Analytics Service Cleanup – Error Reporting
Restructuring Analytics Code
Analytics – Geo and User Agent Info
Analytics – Handling Byte Range Requests
Analytics – Extract The Tracking Code
Analytics – Tracking Download Type
At this point, you will have all the skills you need to write clean Express.js apps, and grow your Node.js architecture to accommodate more users, more features and a larger software development team. While you will need to handle some specifics that have not been covered in this guide, the principles and patterns that you have learned will always help you write clean code.