Design the Routes

A routes function is a callback registered with Express. It’s registered for a URI path and an HTTP method. The callback functions typically take three parameters: HTTP request (req), HTTP response (res), and a next routes handler (next). Unlike the model functions that we typically call from a routes function, we never directly call any of the routes functions in the application code. Instead, Express calls them with an HTTP request. In response, a handler typically renders data to the HTTP response object. This can leave us confused—how in the world can we test a function that’s never called directly from the application code?

Well, our test can pretend to be Express and make the calls to verify the functions’ behavior—it’s ...

Get Test-Driving JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.