CCD-102 practice question 3 of 30

Server.get(‘Show’, consentTracking.consent, cache.applyDefaultCache, function (req,res,next){ Var Site = require(‘dw/system/Syte”); Var…

Full set: 206 questions

Server.get(‘Show’, consentTracking.consent, cache.applyDefaultCache, function (req,res,next){ Var Site = require(‘dw/system/Syte”); Var pageMetaHelpter = require(‘*/cartridge/scripts/helpers/pageMetaHelper’); pageMetaHelpter.setPageMetaTags(req.pageMetaData, Site.current); res.render(‘/home/homePage’); Missing code here }, pageMetadata.computedPageMetadata); The controller endpoint code snippet above does not work. Which line of code should the developer use to replace line 6 and correct the problem?

Answer options
A:

A. next();

Correct answer
B:

B. return res;

C:

C. res.next();

D:

D. req.next();

Correct Answer:
A. A. next();
Explanation

The code snippet utilizes the server module from the Salesforce B2C Commerce Storefront Reference Architecture (SFRA), which follows an Express.js-like middleware pattern. In this pattern, multiple functions can process a request in a specific sequence. The next() function is a callback that passes control to the next middleware function in the chain. In the provided code, pageMetadata.computedPageMetadata is defined as a middleware function to be executed after the main route handler. Therefore, to invoke it and complete the chain, the main handler must call next(); after it has finished its own processing (like res.render).

Why Incorrect

B. return res;: Returning the response object does not pass control to the next middleware. The function's return value is typically ignored by the server module's routing mechanism.
C. res.next();: This is syntactically incorrect. The next function is passed as a direct argument to the middleware handler, not as a method of the res (response) object.
D. req.next();: This is syntactically incorrect. The next function is passed as a direct argument to the middleware handler, not as a method of the req (request) object.

References

1. Salesforce B2C Commerce Documentation: In the Storefront Reference Architecture (SFRA) documentation

the section on controllers explicitly describes this behavior. It states

"The server module uses the Express.js routing system... Each argument after the route name is a function that is executed in order. These functions are sometimes called middleware... To continue to the next function in the chain

the function must call the next() method."

Source: Salesforce Help Portal

B2C Commerce

Develop for SFRA

"Controllers in SFRA".

2. Express.js Official Documentation: The SFCC server module's design is based on Express.js. The official Express.js documentation on middleware clearly defines the role of next(). "A middleware function... can... Call the next middleware function in the stack."

Source: Express.js Documentation

"Writing middleware for use in Express apps"

expressjs.com/en/guide/writing-middleware.html.

3. University Courseware (MIT OpenCourseWare): Courses on modern web development frequently cover the Node.js/Express middleware pattern. Lecture notes explain that next() is the essential mechanism for invoking the subsequent function in the request-handling pipeline.

Source: MIT OpenCourseWare

6.170 Software Studio

Spring 2013

Lecture 15: "Node.js and Express"

Section on "Middleware".

Scroll to Top