Configuring Controllers in Sfcc


A controller is responsible to interact with the application. When a user requests the application controller determines what response to send back. A controller is made up of a route name and action, for example, If you navigate you are calling About Controller.

Controllers are the scripts that control application flow and update application data, The controller creates the View Model of the user data. View Model requests the data from the platform and creates the layer of pure JSON objects to be used by the controller. In simple terms view models are objects we create to send the data to the view or rendering templates

View Passes the call to the controller, the controller asks for the data from the Model, Model sends data back to the Controller. After receiving the data from the model controller creates the view model using data from the model finally view model object is sent from the controller to populate the view.

Till now, we spoke about what controller and view models are now it’s time to check out all parts of the controller –

  • Import statements are used to “require” the function/classes in the controller. We require files at the top of the controller.
var server = require('server');
var fs = require("fs")
  • Endpoints are used to match the incoming request with the defined endpoint in the controller, for example, GET is an endpoint.
server.get('Show', function (req, res, next) {

  • Routes define the string which will be used in the URL for calling the controller. A controller can be called with the Filename or Route name. example- Homepage-Show, here the Show is Route.

Middleware is the function that has access to the request and response object. Whenever you request the data from the application. The request object has access to the user data and parameters you can also modify the response object which can be modified before the server responds to you back. For example, Authentication of a user is like a user providing OTP which has access to auth middleware which verifies the OTP and sends the response back to the user.

You can also write the logic in controllers like what data you want from render, providing additional API

With the help of the controller we can pass data to view or render, a template with a response object we can render a template when use hits a specific endpoint.

var server = require('server');
var fs = require("fs")

server.get('Show', function (req, res, next) {

module.exports = server.exports();

We have discussed controllers, SFRA also provides the feature to extend controllers. Controllers can be extended in the following ways.

  • Server. Append– In case you want to add additional logic at the end of the function you can use the append method, your custom code logic will run right after the next() middleware call
  • Server. Prepend– A piece of code is supposed to be executed at the beginning of the function prepend method comes in handy to execute your custom code first
  • Server. Replace– Replace method can be used when you want to change the entire functionality of the controller.

It was all about controllers, we will get into more details about controllers in future posts.
You have now all you need to start working with the controller and writing your first controller and rendering your first Hello World!

About the author

Kartik Narang
By Kartik Narang