Connecting Database to Existing API (GET)
Свайпніть щоб показати меню
So far, data may have been stored in arrays or temporary variables. Now you replace that logic with database queries.
Instead of returning hardcoded data, you use the model to fetch data from MongoDB.
app.get('/users', async (req, res) => {
const users = await User.find();
res.json(users);
});
For a single item:
app.get('/users/:id', async (req, res) => {
const user = await User.findById(req.params.id);
res.json(user);
});
Examples:
'/users': returns all users from database;'/users/123': returns user with id 123 from database.
This replaces in-memory data with real database data.
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 1. Розділ 11
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Секція 1. Розділ 11