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") })
Leave a Reply