Search API with multiple filed - NODE JS
config.js -
const mongoose=require('mongoose')
mongoose.connect('mongodb://localhost:27017/e-Comm')
Product.js:
const mongoose=require('mongoose');
const productSchema=new mongoose.Schema({
name:String,
price:Number,
brand:String,
category:String
});
module.exports=mongoose.model('products', productSchema);
index.js:
const express=require('express');
require('./config');
const product=require('./product');
const app=express();
app.use(express.json());
app.post('/create', async(req, resp)=>{
let data=new product(req.body);
let result=await data.save();
console.log(result)
resp.send(result);
});
app.get('/list',async (req, resp)=>{
let data=await product.find();
resp.send(data);
});
app.delete('/delete/:_id',async (req, resp)=>{
console.log(req.params)
let data= await product.deleteOne(req.params);
resp.send(data);
});
app.put("/update/:_id", async(req, resp)=>{
console.log(req.params)
let data=await product.updateOne(
req.params,
{
$set:req.body
}
);
resp.send(data);
})
app.get('/search/:key', async (req,resp)=>{
console.log(req.params.key)
let data=await product.find(
{
"$or":[
{"name":{$regex:req.params.key}},
{"brand":{$regex:req.params.key}}
]
}
)
resp.send(data)
})
app.listen(5000);
Comments
Post a Comment