This community-built FAQ covers the “Sending A Response” exercise from the lesson “Learn Express Routes”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Web Development
Learn Express
FAQs on the exercise Sending A Response
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? 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!
Please explain what const { seedElements} = require('./utils');
is and what does it do in relation to:
const expressions = [];
seedElements(expressions, 'expressions');
6 Likes
Hi there,
in app.js we find this:
const { seedElements } = require(’./utils’);
It appears to import a single property of the exported module (object) from utils.js:
module.exports = {
createElement: createElement,
getIndexById: getIndexById,
getElementById: getElementById,
updateElement: updateElement,
seedElements: seedElements,
};
It is smart but was not covered in the module course so it’s all new to me and it appears to use the bracket syntax introduced by ES6 for named exports/imports but does not use the import keyword.
Is this syntax valid in ES5?
If not and it is a ES6 syntax, then why use the require() function rather than the import…from keywords (import { seedElements } from ./utils), making a mix of the two syntaxes?
Since the module.exports example is confusing with properties’ names and values bearing the exact same designation, I’m left to suppose that in const { seedElements }, seedElements is the property’s name, used as a key to locate and return its value. Am I correct?
1 Like
Hi,
In this exercise “Learn Express” (code snippet below), what is the point to call app.use() twice?
const express = require(‘express’);
const app = express();
const { seedElements } = require(’./utils’);
// Serves Express Yourself website
app.use(express.static(‘public’));
const PORT = process.env.PORT || 4001;
// Use static server to serve the Express Yourself Website
app.use(express.static(‘public’));
const expressions = ;
seedElements(expressions, ‘expressions’);
// Get all expressions
app.get(’/expressions’, (req, res, next) => {
// console.log(req);
});
app.listen(PORT, () => {
console.log(Listening on port ${PORT}
);
});
1 Like
seedElements(expressions, “expressions”) is called and it populates the expressions array with items.
let expressionIdCounter = 0;
const createElement = (elementType, queryArguments) => {
if (queryArguments.hasOwnProperty('emoji') &&
queryArguments.hasOwnProperty('name')) {
let currentId;
if (elementType === 'expressions') {
expressionIdCounter += 1;
currentId = expressionIdCounter;
} else {
animalIdCounter += 1;
currentId = animalIdCounter;
}
return {
'id': currentId,
'emoji': queryArguments.emoji,
'name': queryArguments.name,
};
} else {
return false;
}
};
const seedElements = (arr, type) => {
if (type === 'expressions') {
arr.push(createElement('expressions', {'emoji': '😀', 'name': 'happy'}));
arr.push(createElement('expressions', {'emoji': '😎', 'name': 'shades'}));
arr.push(createElement('expressions', {'emoji': '😴', 'name': 'sleepy'}));
} else if (type === 'animals') {
arr.push(createElement('animals', {'emoji': '🐶', 'name': 'Pupper'}));
arr.push(createElement('animals', {'emoji': '🐍', 'name': 'Snek'}));
arr.push(createElement('animals', {'emoji': '🐱', 'name': 'Maru'}));
} else {
throw new Error(`seed type must be either 'expression' or 'animal'`);
}
};
const array= [];
seedElements(array, "expressions");
console.log(array);
Hope that helps