Application Development53% of the exam

SFRA controllers, middleware chains and models

How SFRA routes are registered and chained, how res.render, res.json and setViewData shape the output, when to append or replace an inherited route, and where models, helpers and factories belong in a cartridge.

Official exam objective
Given a requirement, create and extend the functionality of a JavaScript controller that leverages models, decorators, factories, or helpers following API best practices, and renders a template or returns a JSON response.

~8 min read · 15 practice questions ·

What the exam expects

  • Write an SFRA controller that registers routes with server.get, server.post or server.use, chains middleware correctly and exports its routes.
  • Choose res.render, res.json or res.setViewData for a response requirement, and predict what the template's pdict contains.
  • Extend an inherited route with server.append, server.prepend or server.replace, and decide which is safe when the original calls a third-party system.
  • Restrict a route with middleware filters (get, post, https, include) and use route events such as route:BeforeComplete.
  • Place logic in models, helpers and factories in a custom cartridge, require modules in the narrowest scope, and handle error-page constraints.

Key terms: route (a Controller-RouteName endpoint registered by the server module); middleware chain (the functions between the route name and the main function, run in order); viewData (the output object that setViewData merges into and the template reads as pdict); model or ViewModel (code that turns Script API objects into plain JSON for the storefront); middleware filter (a provided step that rejects requests that don't match, such as https).

Key facts

Route registration
Detail
server.exports() registers get, post and use functions
Missing next()
Detail
The next step in the chain doesn't run
Output timing
Detail
Template or JSON output is set after every step completes
route:BeforeComplete
Detail
After all middleware, before route:Complete; stores submitted data
server.append
Detail
Original chain runs first, then the new step, before rendering
server.prepend
Detail
Adds middleware ahead of an inherited route's steps
Price factory
Detail
getPrice returns a TieredPrice, RangePrice or DefaultPrice
Filter mismatch
Detail
Error Params do not match route
include filter
Detail
Limits a route to remote includes
Error.js default
Detail
410; 404 only without isslot, iscomponent, isinclude
Controller extension
Detail
.js or .ds, in the cartridge's controllers folder
require() placement
Detail
Most limited scope; global requires load every request
Sources for this section (6)
  • When is route:Redirect emitted?
  • Why would a team unsubscribe from route:Complete?
  • Where does SFRA place parsed query string parameters?
  • When does a request to an existing controller still fail with an error?

The questions are free to read. Answers with their sources and self-grading are included with the pack.

Keep reading with the B2C Commerce Developer pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 28 lessons of the B2C Commerce Developer guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Practice exams purchase adds inline bank questions and topic practice (15 questions for this lesson)
  • An official source cited for every claim
  • An AI tutor that answers from the guide
  • Choose guide-only, practice-only, or save $4 with both

Practice for the B2C Commerce Developer exam