post api call using node js and mongodb code example
Example 1: connect mongo to express api
//using mongoose
npm i mongoose
const mongoose = require("mongoose");
mongoose
.connect(
"your mongodb URI",
{
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
}
)
.then(() => console.log("mongodb connected"))
.catch((err) => {
console.err(err.message);
process.exit(1);
});
Example 2: fetch api based on id nodejs and mongodb
// contactModel.jsvar mongoose = require('mongoose');// Setup schemavar contactSchema = mongoose.Schema({ name: { type: String, required: true }, email: { type: String, required: true }, gender: String, phone: String, create_date: { type: Date, default: Date.now }});// Export Contact modelvar Contact = module.exports = mongoose.model('contact', contactSchema);module.exports.get = function (callback, limit) { Contact.find(callback).limit(limit);}