Controllers are server-side scripts that handle storefront requests. Controllers manage
the flow of data in your application, and create ViewModels
to process each
storefront request as a route
and generate an appropriate response. For
example, in a storefront application, clicking a category menu item or entering a search term
triggers a controller that renders a page.
Controllers are written in JavaScript and Salesforce B2C Commerce script. They must conform to the CommonJS module standard.
A controller's file extension can be either .ds or .js. Controllers must be located in the
controllers
folder at the top level of the cartridge. Exported methods
for controllers must be explicitly made public to be available to handle storefront
requests.
Example: SFRA Base Cartridge Controller Hello.Js
'use strict';
var server = require('server');
var cache = require('*/cartridge/scripts/middleware/cache');
server.get('World', cache.applyDefaultCache, function (req, res, next) {
res.render('helloworld', {
Message: 'Hello World! Again.'
});;
next();
});
module.exports = server.exports();
require
to: var rootFolder = require('dw/content/ContentMgr').getSiteLibrary().root;
While it isn't best practice, controllers can also:
controller-function
. For
example, the request URL for the Hello.js controller World function looks like:
https://localhost/on/demandware.store/Sites-RefArch-Site/default/Hello-World
B2C Commerce has several system names that are reserved and can't be used for custom controllers and their functions. For a full list, see System Pipelines and Controllers.
SFRA uses the server.req
and server.res
objects for
request and response data. While global variables are still available, we don't recommend
that you use them.
Almost any JavaScript-related error gets logged in the
customerror_* log
files, including errors from B2C Commerce scripts and
controllers. The error_*
log files contain Java-related errors on B2C
Commerce, and are probably less useful to non-B2C Commerce employees
Error.js
controller services uncaught errors. By default, B2C Commerce
returns a 410 error, which indicates that the resource is no longer available. If you prefer
404, it's fine to use as long as the related ISML template doesn't include
<isslot>
, <iscomponent>
, or
<isinclude>
tags. When you want to handle these errors explicitly,
you can use try/catch
statements.Performance
Only require modules when you need them, not at the top of a module.
You can build a mobile or native app in addition to your storefront. If you want to create modules that work both for a desktop storefront and a mobile app, use hooks.