An SFRA route is a name, a chain of middleware and a template, joined at run time by server.js. Intellij SFCC joins them in the editor: req and res are typed in every callback, middleware opens in SFRA's modules, an append finds the route it extends, res.getViewData() knows what every step before it set, and the template a route renders gets its model as pdict
req and res complete with their types, so req.currentCustomer.profile and res.render come up as you type. Cmd+Click on server.middleware.https opens the function in your project's modules/server/middleware.jsBefore you start
| Needs | Why |
|---|---|
SFRA's modules folder marked as Modules (Global Modules) Root | require('server') loads it, see B2C modules and global modules |
| A valid license or trial | Route and template completion, navigation, gutter icons and Code Vision are paid features, see Purchase & License |
Typed req and res
Every function passed to server.get, server.post, server.use, server.append, server.prepend and server.replace gets SFRA's types, with no JSDoc:
| Parameter | Type | Members you use most |
|---|---|---|
req | sfra.Request | querystring, httpParameterMap, form, currentCustomer with profile, addressBook, wallet and raw, locale, session, geolocation, pageMetaData |
res | sfra.Response | render, json, setViewData, getViewData, redirect, setStatusCode, cacheExpiration, setHttpHeader, print |
next | sfra.Next | Call it to run the next step |
server.get('Show', server.middleware.https, function (req, res, next) {
var profile = req.currentCustomer.profile; // sfra.Profile
this.on('route:BeforeComplete', function (req, res) {
res.setViewData({ updated: true }); // typed here too
});
res.render('account/loyalty', { points: loyaltyHelpers.getPoints(profile.customerNo) });
next();
});Inside a route function, this.on takes the route events route:Start, route:Redirect, route:Step, route:BeforeComplete and route:Complete, and types the req and res of its callback
server and middleware
| Code | Cmd+Click opens | Completes |
|---|---|---|
require('server') | SFRA's modules/server.js | |
server.get, server.post, server.use, server.replace | The method in modules/server/server.js | get, post, use, replace, middleware, forms, querystring and the other members your SFRA copy defines |
server.middleware.https | The https function in modules/server/middleware.js | https, http, get, post, include and any middleware your copy adds |
server.forms, server.querystring | The assignment in modules/server.js |
Members your SFRA copy doesn't define, such as server.getRoute, open the bundled typings. Middleware of your own cartridges, such as csrfProtection.generateToken or userLoggedIn.validateLoggedIn, comes in through require like any module, see Module Resolution
Extend a route
server.extend(module.superModule) makes a controller an override of the same controller further right on the cartridge path. Then:
| Code | What happens |
|---|---|
server.append(''), server.prepend(''), server.replace('') | Completes the routes of the controller you extend, each with its cartridge |
Cmd+Click on 'Show' in an append or prepend | Opens the route it extends, in the nearest cartridge further right on the site's cartridge path |
Cmd+Click on 'Show' in server.get('Show', ...) | Lists every other definition, append, prepend and replace of the route |
| The lenses above a route | append (1), prepend (2) or replace (1) list the cartridges that extend it, see SFRA extensions |
To find a route from anywhere, use the B2C Entry Points tab of Search Everywhere. For URLUtils.url('Cart-Show') and other route strings, see Routes and controllers
Route calls that throw
SFRA throws when the controller loads if server.get, server.post or server.use adds a route the server already holds, or if server.replace, server.append or server.prepend changes one it doesn't hold. The SFRA route defined again, or changed before it exists inspection turns the route name red:
| Code | Message |
|---|---|
server.get('Show', ...) in a controller that extends one with Show | Route 'Show' is already defined in app_storefront_base; server.get() throws for an existing route, override it with server.replace() |
A second server.get('Show', ...) in the same file | Route 'Show' is already defined on line 12; server.get() throws for an existing route |
server.append('Show', ...) when the controllers below have no Show | Route 'Show' is not defined on this server; server.append() throws for a missing route |
server.append('Show', ...) in a controller that doesn't call server.extend(module.superModule) | Route 'Show' is not defined on this server; app_storefront_base defines it, but this controller does not call server.extend(module.superModule) |
For a route from a controller below, the quick fix Override with server.replace() and server.middleware.get turns server.get('Show', ...) into server.replace('Show', server.middleware.get, ...). replace adds no method filter of its own, so the fix keeps get or post as middleware. On server.use it reads Override with server.replace()
Calls inside a condition, such as if (!server.getRoute('Show')), route names the code doesn't decide and extended controllers that can't be found aren't reported. The inspection checks the controllers of linked cartridges, needs a valid license or trial, and sits under Settings › Editor › Inspections › SFCC › Routes
Typed view data
res.getViewData() and res.viewData have the type of what the route has set at that line. The plugin reads the route the way the call's own controller builds it: the steps of its cartridge and of the copies below it on the site's cartridge path, through every server.extend(module.superModule), each server.prepend before the steps it extends and each server.append after them. A cartridge further left on the path adds nothing, not even its prepends: it runs only on the sites that have it, so the code below it can't count on what it sets
server.get('Show', function (req, res, next) {
var product = ProductFactory.get(req.querystring);
res.render('product/productDetails', { product: product, addToCartUrl: URLUtils.url('Cart-AddProduct') });
next();
});server.extend(module.superModule);
server.append('Show', csrfProtection.generateToken, function (req, res, next) {
var viewData = res.getViewData(); // { product, addToCartUrl, csrf }
viewData.reviews = reviewHelpers.getReviews(viewData.product.id);
res.setViewData({ reviewCount: viewData.reviews.length });
next();
});Each member has the type of the value that sets it, so viewData.product. completes the product model and viewData.addToCartUrl is a dw.web.URL. A member that several steps set has the union of their types
| Code in a step | Adds |
|---|---|
res.setViewData({ reviewCount: count }) | reviewCount |
res.render('product/productDetails', model), res.json(model), res.page(pageId, model) | The members of an object literal model, or of a variable built from one |
res.xml(xmlString) | xml |
viewData.reviews = reviews on the object res.getViewData() returns | reviews |
A middleware of the route, such as csrfProtection.generateToken | What it sets, up to 50 functions deep. A */cartridge/... module is the first copy at or below the step's cartridge |
What a call sees depends on where it is:
| Place | Sees |
|---|---|
| A step of the route | What the steps before it set, and its own lines above the call |
A this.on('route:BeforeComplete', ...) listener | What the whole chain sets, from its own cartridge down |
A function of the controller that a route passes by name, such as check in server.get('Edit', check, ...) | What the steps before it set |
| Any other function | What it set itself, and any other name as any |
The type works like any other object type:
viewData.completes the members, andCmd+Clickon one opens the line that sets it, or lists every step that sets it- Find Usages on a model property, such as
productin the baseres.render, lists theviewData.productreads in every cartridge - The type hint after
var viewDatafolds a big object to{...}. Click it to open the members in place - A step added, changed or removed in the call's cartridge or one below it changes the type at once. The chain is read from the same index as
pdict, so no other controller is parsed
A helper the plugin can't follow, such as decorate(req, res), or a model it can't read, such as res.json(result), keeps the members it knows and accepts any other name. Typed view data works in the .js controllers of linked cartridges and needs a valid license or trial
View data in templates
res.setViewData({ loyaltyPoints: points }) in an append adds pdict.loyaltyPoints to the template the base route renders, in its own cartridge or any other. So does viewData.loyaltyPoints = points on the object res.getViewData() returns. The plugin reads both across the route's handlers, every server.append and server.prepend of it and its middleware, up to 50 functions deep, so csrfProtection.generateToken adds pdict.csrf
In the template, pdict. completes the member with its type, and quick documentation (F1, Ctrl+Q) names the line that sets it. A middleware or helper the plugin can't follow, such as decorate(req, res), keeps the members it knows and stops the unknown member warning, see Render calls
Picked templates
A route that picks its template at run time keeps every link of a literal path:
var template = req.querystring.panel ? 'account/dashboardPanel' : 'account/accountDashboard';
res.render(template, { account: accountHelpers.getAccountModel(req) });| Form | Example |
|---|---|
A variable, also one set in if and else | var view = 'a/mobile', then res.render(view, model) |
?: | res.render(isGuest ? 'checkout/guest' : 'checkout/login', model) |
|| and ?? | res.render(req.querystring.view || 'search/searchResults', model) |
Each path completes template paths, gets the Navigate to template gutter icon , opens with Cmd+Click and is found and renamed with its template. The model joins the pdict of each template. With copies in several cartridges the icon reads Navigate to template (2 cartridges) and opens Choose Template, in the order the server tries them, see Gutter icons
A path built from parts, such as 'account/' + name, or passed in as a function parameter, isn't followed
Troubleshooting
| Symptom | What to do |
|---|---|
req. or res. lists nothing in a route | Mark SFRA's modules folder as Modules (Global Modules) Root and check the license, see Before you start |
server.append('') lists nothing | Add server.extend(module.superModule) and check that another linked cartridge has a controller with the same name |
| An append opens no base route | The base controller is in a cartridge that isn't linked, or has another file name |
A route name in server.get or server.append is red | The call throws when the controller loads, see Route calls that throw |
res.getViewData() accepts any name | A step passes the response to a helper the plugin can't follow, or renders a model it can't read, see Typed view data |
A pdict member from an append is missing in the template | Set it with res.setViewData({...}) or on the object res.getViewData() returns, in the route's own handler, an append or a middleware |
| A picked template has no gutter icon | The path is built from parts or passed in, see Picked templates |