Skip to content

Commit

Permalink
Search API in node js
Browse files Browse the repository at this point in the history
  • Loading branch information
codingXpert committed Jul 31, 2022
1 parent 7da9d79 commit e8e6295
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/e-comm");
mongoose.connect("mongodb://localhost:27017/e-comm");
1 change: 1 addition & 0 deletions product.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ const productSchema = new mongoose.Schema({

//Model
module.exports = mongoose.model('products' , productSchema);
// **********************************
25 changes: 25 additions & 0 deletions search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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


//search API
app.get("/search/:key" , async(req,res)=>{
console.log(req.params.key);
let data = await product.find(
{
"$or":[ //here "or" is used for multiple conditions
{"name":{$regex:req.params.key}},
{"brand":{$regex:req.params.key}},
{"category":{$regex:req.params.key}}
]
}
)
res.send(data);
});

const port = 8000;
app.listen(port);

0 comments on commit e8e6295

Please sign in to comment.