Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scalable REST API in Node.js

Scalable REST API in Node.js

Mehdi Hasan Khan

July 06, 2019
Tweet

More Decks by Mehdi Hasan Khan

Other Decks in Programming

Transcript

  1. Who am I - Hi, I’m Mehdi Hasan Khan !

    - Currently Software Architect @ ShopUp 
 We’re a JavaScript shop and we are hiring - Writing JavaScript professionally for 9 years - Developer of Avro Keyboard - Dad of a wonderful kid
 
 Twitter: @MehdiHK GitHub: https://github.com/mugli LinkedIn: https://www.linkedin.com/in/mehdihk/
  2. In real world your database and other backend services are

    probably the bottleneck. Not the HTTP Framework.
  3. Common API Life Cycle • Build • Design • Develop

    • Document • Test • Use • Access Control • Data Access • Run • Scale with traffic • Availability • Monitoring
  4. Most badly designed REST APIs aren't bad because they were

    developed by bad coders. It's because they were not designed in the first place, they were just developed.
  5. As the flexibility of a system increases, its usability decreases

    Flexibility-Usability Tradeoff Universal Principles of Design
  6. • Include .json extension or not in endpoints? • CamelCase

    or snake _case? • Version in the url or header or param? • Verbs are bad? How do I make search endpoints? • Should GET requests have body? • Do you even paginate bro? Cursor or offset? • Formatted output or minified? • How do I avoid over-fetching?
  7. Better: hapi and joi • Correct status code • Auto-generated

    error message • Consistent error object • Declarative syntax, no ugly regex hacks, type checks • What’s expected is immediately visible • Makes refactoring easy for other API developers • Can be used to check response object too before they are send to the client! 
 (Not shown in the example)
  8. The OpenAPI Specification defines a standard, programming language-agnostic interface description

    for REST APIs, which allows both humans and computers to discover and understand the capabilities of a service without requiring access to source code, additional documentation, or inspection of network traffic.
  9. • hapi searches paths in order from most specific to

    least specific. • If you have two routes, /filename.jpg and /filename.{ext} a request to /filename.jpg will match the first route, and not the second, whatever their order is in code. • That also mean, a route with the path /{files*} will be the last route tested, and will only match if all other routes fail.
  10. • hapi has deterministic routing. Each request can only map

    to one route, and its routing table will be the same every time you start the server. • As the application size and teams grow, routing conflicts become more of a concern. You want a banana but you get a gorilla holding the banana and the entire jungle. • If you have two routes that conflict, hapi will show an error on startup, providing details on the routes that conflict, making it much easier to debug and fix. This is much better than spending hours debugging this at runtime.
  11. You want to test the route • You don’t need

    to spawn the whole server • You don’t need to listen to a socket • You don’t even need to send a real http call and process it • Because you are not testing if the underlying http stack is working in Node.js, you are testing if you pass some values to your endpoint, if it returns correct result
  12. Final thoughts • Express is minimal and optimized for quick

    learning and prototyping. Unfortunately it became the most popular framework in Node.js ecosystem for the same reason. There are better options though. • Hapi brought a lot of battle tested and production ready practices to Node.js • Fastify had the luxury of coming later and learnt from both of them. • If you are stuck with existing express based api, there are middlewares that tries to make it better.