Skip to content

Commit

Permalink
post API with mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
codingXpert committed Jul 31, 2022
1 parent dd2dad9 commit 8d702e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Index3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');
require ('./config');
const product = require('./product');

const app = express();
app.use(express.json()) //converts the data into JSON formate forom stream formate

app.post("/create" , async (req,res)=>{
let data = new product(req.body);
let result = await data.save();
console.log(result);
res.send(result);
});

const port= 8000;
app.listen(port);
2 changes: 2 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/e-comm");
14 changes: 14 additions & 0 deletions product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//Here we define Schemas and Models

const mongoose = require('mongoose');

//Schema
const productSchema = new mongoose.Schema({
name:String,
brand:String,
price:Number,
category:String
});

//Model
module.exports = mongoose.model('products' , productSchema);

0 comments on commit 8d702e6

Please sign in to comment.