FAQ: Code Challenges - Code Challenge

This community-built FAQ covers the “Code Challenge” exercise from the lesson “Code Challenges”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Express

FAQs on the exercise Code Challenge

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

const timeMiddleware = (req, res, next) => {
req.date = Date.now();
next();
};

can anyone explain me why we’ve written req.date?
is req.date like a variable?

I don’t really understand ‘req.date’.

1 Like

req represents a request object. We add the date property to the request object so that as the request object gets passed between middleware functions, the date is always available to the functions that need it. Perhaps a global variable could be set in the application instead, but there are multiple downsides to that, especially with a large, complex application.

Perhaps you could think of the property as being like a variable attached to an object. The official documentation shows that there are many such properties already associated with the request object: https://expressjs.com/en/api.html#req We just added a custom one of our own.

2 Likes

The instructions read:
_ Complete the timeMiddleware function to DRY the current application by attaching the currentTime to the request body as date . Use this in all routes and eliminate the duplicate code._

Hence I too thought this answer would work. See below and help me understand why Codecademy throws and error. Thanks for any help.

const timeMiddleware = (req, res, next) => {

req.currentTime = Date.now();

next();

};

app.use(timeMiddleware);

app.get(’/snacks’, (req, res, next) => {

//const currentTime = Date.now();

res.send(Snacks as of ${req.currentTime}: ${database.snacks});

});

app.get(’/sides’, (req, res, next) => {

//const currentTime = Date.now();

res.send(Sides as of ${req.currentTime}: ${database.sides});

});

app.get(’/appetizers’, (req, res, next) => {

//const currentTime = Date.now();

res.send(Appetizers as of ${req.currentTime}: ${database.appetizers});

});

i.e why doesn’t “currentTime” work in place of “date”?
Is “date” a predefined Express property to be wielded as needed?

Here is the actual solution accepted by Codecademy software.

const express = require(‘express’);

const app = express();

const database = {

snacks: [‘chips’, ‘apple’, ‘cookies’],

sides: [‘beans and rice’, ‘cole slaw’, ‘broccoli’],

appetizers: [‘oysters’, ‘dumplings’, ‘smoked almonds’],

};

// Add your code here:

const timeMiddleware = (req, res, next) => {

req.date = Date.now();

next();

};

app.use(timeMiddleware);

app.get(’/snacks’, (req, res, next) => {

res.send(Snacks as of ${req.date}: ${database.snacks});

});

app.get(’/sides’, (req, res, next) => {

res.send(Sides as of ${req.date}: ${database.sides});

});

app.get(’/appetizers’, (req, res, next) => {

res.send(Appetizers as of ${req.date}: ${database.appetizers});

});

I’m not sure. I don’t have Pro access anymore, so I can’t easily try this out.

I think that “date” is not a special Express property, but that the Codecademy testing software was hardcoded to look for req.date in solutions. Sometimes the checks are just too specific. If that’s the case, then I think “currentTime” would work in your own (non-Codecademy) environment barring any namespace issues.

2 Likes
const express = require('express');
const app = express();

const database = {
  snacks: ['chips', 'apple', 'cookies'],
  sides: ['beans and rice', 'cole slaw', 'broccoli'],
  appetizers: ['oysters', 'dumplings', 'smoked almonds'],
};

// Add your code here:
const timeMiddleware = (req, res, next) => {  
  req.date = Date.now();
  next();
};

app.use(timeMiddleware);

app.get('/snacks', (req, res, next) => {
  res.send(`Snacks as of ${req.date}: ${database.snacks}`);
});

app.get('/sides', (req, res, next) => {
  res.send(`Sides as of ${req.date}: ${database.sides}`);
});

app.get('/appetizers', (req, res, next) => {
  res.send(`Appetizers as of ${req.date}: ${database.appetizers}`);
});

Hi i have tried to complete the3rd challenge with the following code:
const express = require(‘express’);
const app = express();

const database = {
snacks: [‘chips’, ‘apple’, ‘cookies’],
sides: [‘beans and rice’, ‘cole slaw’, ‘broccoli’],
appetizers: [‘oysters’, ‘dumplings’, ‘smoked almonds’],
};

// Add your code here:
const timeMiddleware = (req,res,next) => {
const currentTime = Date.now();
req.currentTime = currentTime;
next();
};

app.get(‘/snacks’, timeMiddleware, (req, res, next) => {
res.send(Snacks as of ${req.currentTime}: ${database.snacks});
});

app.get(‘/sides’, timeMiddleware, (req, res, next) => {
res.send(Sides as of ${req.currentTime}: ${database.sides});
});

app.get(‘/appetizers’, timeMiddleware, (req, res, next) => {
res.send(Appetizers as of ${req.currentTime}: ${database.appetizers});
});

I understand that the correct solution is using the timeMiddleware within app.use, however, shouldn’t this also work? i remember passing the middleware function as the 2nd callback inside app.get functions should also do the job.
Cheers