Adding Searching Functionality In Node
In this article we see how we can enable the searching of data from server in our node application.
Setup The Folder
a) Make a new folder using command prompt.Type the following command.
Add New Folder
for more "$regex" usage see this link
Setup The Folder
a) Make a new folder using command prompt.Type the following command.
- mkdir search
- cd search
Setup Node In Folder
For setting up node we type the following command
- npm init -y
it will initialize the node in our project.
And we get a package.json file in our folder.
Which means node is initialized in our folder.
Install Packages
Now we install the packages which will be required for building the application.
- npm install express ejs body-parser mongoose.
- models
- views
- models - this will contain our schema of collections(table).
- views - will contain the ejs pages.
Model
Add new file in folder name it data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var bookSchema = new mongoose.Schema({ | |
author:{ | |
type:String | |
}, | |
books:{ | |
type:String | |
} | |
}); | |
module.exports = mongoose.model('books',bookSchema); |
Views
Add 2 folders
a)pages
b)partial
In partial folder add 2 new files header.ejs and footer.ejs
header.ejs
footer.ejs
In pages add home.ejs file.
home.ejs
Add New File
Add 2 folders
a)pages
b)partial
In partial folder add 2 new files header.ejs and footer.ejs
header.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Searching</title> | |
<link rel="stylesheet" href="https://bootswatch.com/4/pulse/bootstrap.css"> | |
<link rel="stylesheet" href="https://bootswatch.com/_assets/css/custom.min.css"> | |
</head> | |
<body> | |
<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top"> | |
<a class="navbar-brand" href="#">Searching</a> | |
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation"> | |
<span class="navbar-toggler-icon"></span> | |
</button> | |
<ul class="navbar-nav mr-auto"> | |
<li class="nav-item active"> | |
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Features</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Pricing</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">About</a> | |
</li> | |
</ul> | |
<div class="collapse navbar-collapse nav justify-content-end" id="navbarColor01"> | |
<form class="form-inline my-2 my-lg-0" action="/search" method="get"> | |
<input class="form-control mr-sm-2" type="text" name="dsearch" placeholder="Search"> | |
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button> | |
</form> | |
</div> | |
</nav> |
In pages add home.ejs file.
home.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%-include('../partial/header')-%> | |
<div class=" container"> | |
<center> | |
<div class="card" style="width: fit-content;"> | |
<div class="card-body"> | |
<form action="/" method="POST"> | |
<div class="row"> | |
<div class="form-group col-md-12"> | |
<input type="text" class="form-control" name="author" placeholder="Auhtor Name" required> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="form-group col-md-12""> | |
<input type="text" class="form-control" name="book" placeholder="Book Name" required> | |
</div> | |
</div> | |
<button type="submit">Submit</button> | |
</form> | |
</div> | |
</div> | |
</center> | |
<br> | |
<%if(data.length>0){%> | |
<center> | |
<div style="width:auto; border-style: solid;border-color: black;"> | |
<table class="table table-border table-hover"> | |
<thead class="bg-warning"> | |
<tr> | |
<th> | |
s.no | |
</th> | |
<th> | |
Auhtor | |
</th> | |
<th> | |
Books | |
</th> | |
</tr> | |
</thead> | |
<tbody> | |
<%for(var i=0;i< data.length;i++){%> | |
<tr> | |
<td> | |
<%=i+1%> | |
</td> | |
<td> | |
<%=data[i].author%> | |
</td> | |
<td> | |
<%=data[i].books%> | |
</td> | |
</tr> | |
<%}%> | |
</tbody> | |
</table> | |
</div> | |
</center> | |
<%}%> | |
</div> | |
<%-include('../partial/footer')-%> |
Add New File
Add new file name it app.js.
This Will be the entry point of our application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var bodyParser = require('body-parser'); | |
var express = require('express'); | |
var bookModel = require('./models/data'); | |
//connect to db | |
mongoose.connect('mongodb://localhost:27017/searchingg',{useNewUrlParser:true}) | |
.then(()=>console.log('connectd to db')) | |
.catch((err)=>console.log('error ',err)) | |
//init app | |
var app = express(); | |
//set view engine | |
app.set('view engine','ejs'); | |
///fetch the data from request | |
app.use(bodyParser.urlencoded({extended:false})); | |
//default page load | |
app.get('/',(req,res)=>{ | |
try { | |
bookModel.find((err,data)=>{ | |
if(err){ | |
console.log(err) | |
}else{ | |
res.render('pages/home',{data:data}); | |
} | |
}); | |
} catch (error) { | |
console.log(error); | |
} | |
}); | |
//search | |
app.get('/search',(req,res)=>{ | |
try { | |
bookModel.find({$or:[{author:{'$regex':req.query.dsearch}},{books:{'$regex':req.query.dsearch}}]},(err,data)=>{ | |
if(err){ | |
console.log(err); | |
}else{ | |
res.render('pages/home',{data:data}); | |
} | |
}) | |
} catch (error) { | |
console.log(error); | |
} | |
}); | |
app.post('/',(req,res)=>{ | |
try { | |
var books = new bookModel({ | |
author:req.body.author, | |
books:req.body.book | |
}); | |
books.save((err,data)=>{ | |
if(err){ | |
console.log(err) | |
}else{ | |
res.redirect('/'); | |
} | |
}) | |
} catch (error) { | |
console.log(error); | |
} | |
}); | |
var port = process.env.PORT || 3000; | |
app.listen(port,()=>console.log('server run at '+port)); |
for more "$regex" usage see this link
Comments
Post a Comment