0
0
Expressframework~20 mins

DTO pattern for data transfer in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DTO Mastery in Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main purpose of a DTO in Express applications?

In Express, why do developers use a Data Transfer Object (DTO)?

ATo store session data on the server for each user automatically.
BTo define a strict structure for data sent between client and server, improving validation and security.
CTo directly connect the database models with the user interface without any changes.
DTo replace middleware functions for routing requests.
Attempts:
2 left
💡 Hint

Think about how data is shaped and controlled when moving between parts of an app.

component_behavior
intermediate
2:00remaining
What output does this Express DTO code produce?

Given this DTO and Express route, what will the server respond with when sending {"name": "Alice", "age": 30, "extra": "ignore"}?

Express
class UserDTO {
  constructor(data) {
    this.name = data.name;
    this.age = data.age;
  }
}

app.post('/user', (req, res) => {
  const userDto = new UserDTO(req.body);
  res.json(userDto);
});
A{"name":"Alice","age":30}
B{"name":"Alice","age":30,"extra":"ignore"}
C{"name":"Alice"}
D{}
Attempts:
2 left
💡 Hint

Look at which properties the DTO constructor copies from the input.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a DTO class in Express to accept only 'title' and 'content' fields?

Choose the correct DTO class that safely extracts only 'title' and 'content' from input data.

A
class PostDTO {
  constructor(data) {
    this.title = data.title;
    this.content = data.content;
  }
}
B
class PostDTO {
  constructor(data) {
    this.title = data.title;
    this.content = data.content;
    this.author = data.author;
  }
}
C
class PostDTO {
  constructor(data) {
    this.title = data.title;
    this.content = data.content;
    this.date = new Date();
  }
}
D
class PostDTO {
  constructor(data) {
    this.title = data.title;
    this.content = data.content;
    this.tags = data.tags;
  }
}
Attempts:
2 left
💡 Hint

Only include the fields 'title' and 'content' exactly as requested.

🔧 Debug
advanced
2:00remaining
What error does this DTO code cause in Express?

Identify the error when this DTO is used in an Express route:

class ProductDTO {
  constructor(data) {
    this.name = data.name;
    this.price = data.price;
  }
}

app.post('/product', (req, res) => {
  const product = new ProductDTO();
  res.json(product);
});
ANo error, returns empty object
BSyntaxError: Unexpected token in class definition
CTypeError: Cannot read property 'name' of undefined
DReferenceError: ProductDTO is not defined
Attempts:
2 left
💡 Hint

Check how the constructor is called and what arguments it expects.

state_output
expert
2:00remaining
What is the final value of 'filteredData' after applying this DTO function?

Consider this function that uses a DTO pattern to filter input data:

function filterUserData(input) {
  const allowedFields = ['username', 'email'];
  const filteredData = {};
  for (const key in input) {
    if (allowedFields.includes(key)) {
      filteredData[key] = input[key];
    }
  }
  return filteredData;
}

const input = { username: 'bob', email: 'bob@example.com', password: 'secret' };
const filteredData = filterUserData(input);

What is the value of filteredData?

A{}
B{"username":"bob","email":"bob@example.com","password":"secret"}
C{"password":"secret"}
D{"username":"bob","email":"bob@example.com"}
Attempts:
2 left
💡 Hint

Only keys in allowedFields should be included in filteredData.