Create API Connecting to MongoDB Part 1

Create API Connecting to MongoDB Part 1

In this Artcle i will show you how to create API to connect to Mongo DB  Database step by step.

First Create a Folder call  shop-api

Anyway you can name the Folder what ever name you wanted

 

Open Terminal and navigate to the folder ” shop-api ”

Initialize Node Package Manager

Node Package Manager will  walk you through to create the JSON File

 

Install Body-parser

Install Mongoose express

 

 

When Done Check your File, it should contain all the files in the picture below

 

 

Open Fie ” Server.js” and write the code below

 

var express = require('express');
var app =express();
var bodyParser =require('body-parser');
var mongoose =require('mongoose');
var db = mongoose.connect('mongodb://localhost/shop-api');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

app.listen(3000,function(){
    
    console.log("Shop API running")
    
})



Check whether the API is connected

Check out how to find and remove data form Mongo DB here

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × four =