Slide 12
Slide 12 text
Server With Mongoose
app.post('/persons', async (req, res) => {
try {
// Extract person data from the request body
const { name, age, gender } = req.body;
// Create a new person document using the Person model
const person = new Person({ name, age, gender });
// Save the person to the database
await person.save();
res.status(201).json({ message: 'Person saved successfully' });
} catch (error) {
console.error('Error saving person:', error);
res.status(500).json({ error: 'Error saving person' });
}
});