Generate APIs
How to use generative AI to generate API services
Example: Generating an API Endpoint to Retrieve Bicycle Rentals from a MongoDB Database
Prompt
generate a Node.js API endpoint to retrieve bicycle rental information from a MongoDB database, bicycle-app-configResponse
// Generated Node.js API endpoint to retrieve bicycle rental information from a MongoDB database
app.get('/bicycle-rentals', function(req, res) {
const rentalId = req.query.rentalId;
const db = client.db('bicycle-rental-db');
const rentalsCollection = db.collection('rentals');
if (rentalId) {
rentalsCollection.findOne({ rentalId: rentalId }, function(err, rental) {
if (err) throw err;
res.send(rental);
});
} else {
rentalsCollection.find({}).toArray(function(err, rentals) {
if (err) throw err;
res.send(rentals);
});
}
});Discussion
Last updated