Skip to main content

Command Palette

Search for a command to run...

How to create a POST api With Mongo DB and Node JS

Published
1 min readView as Markdown
How to create a POST api With Mongo DB and Node JS

first you have to build connection with you database for that will create db.js file you have to import this file in you main file named as index.js or server.js file you can see that in bottom code snippets const mongoose = require("mongoose"); const express = require("express"); require("dotenv").config(); const app = express(); app.use(express.json()); app.use(express.urlencoded({ extended: true })); mongoose.connect(process.env.MONGODBURI, { useNewUrlParser: true, useUnifiedTopology: true, }); const db = mongoose.connection; db.once("open", () => { console.log("Database connected:", process.env.MONGODB_URI); }); db.on("error", (err) => { console.error("connection error:", err); }); Create a usermodel.js file in your node js project const moongoose = require("mongoose"); const userSchema =

Read Full Article

More from this blog

S

Solving Matters | AI News Aggregator

2206 posts

Stay updated with the latest trends and news in the world of AI.