Creating a basic search engine with Node.js and Express involves setting up a search index, implementing search functionality, and displaying search results. Below is a step-by-step guide to help you build a simple search engine.
Step 1: Set Up Your Project
Create a new project folder:
mkdir search-engine cd search-engine
Initialize a new Node.js project:
npm init -y
Install necessary dependencies:
npm install express ejs lunr
Step 2: Create Your Folder Structure
Create the following folder structure:
search-engine/
|-- src/
| |-- views/
| |-- index.ejs
| |-- routes/
| |-- search.js
| |-- data/
| |-- sample-data.json
| |-- app.js
|-- server.js
|-- .gitignore
|-- package.json
Step 3: Set Up Express Server
In app.js
, set up an Express server:
const express = require('express');
const path = require('path');
const searchRoutes = require('./routes/search');
const app = express();
const PORT = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
// Routes
app.use('/search', searchRoutes);
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Step 4: Create Search Routes
In routes/search.js
, set up routes for handling search functionality:
const express = require('express');
const router = express.Router();
const lunr = require('lunr');
const fs = require('fs');
const path = require('path');
const dataPath = path.join(__dirname, '../data/sample-data.json');
// Load sample data
const rawData = fs.readFileSync(dataPath, 'utf-8');
const sampleData = JSON.parse(rawData);
// Create Lunr index
const searchIndex = lunr(function () {
this.ref('id');
this.field('title');
this.field('content');
sampleData.forEach(function (item, index) {
item.id = index;
this.add(item);
}, this);
});
router.get('/', (req, res) => {
const query = req.query.q;
let results = [];
if (query) {
results = searchIndex.search(query).map(result => sampleData[result.ref]);
}
res.render('index', { query, results });
});
module.exports = router;
Step 5: Create Views
In views/
, create the following EJS template:
index.ejs
: Display the main page with a search form and search results.
Step 6: Create Main Page
In views/index.ejs
, create the main page:
<!-- views/index.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Engine</title>
</head>
<body>
<h1>Search Engine</h1>
<form action="/search" method="get">
<label for="searchQuery">Search:</label>
<input type="text" id="searchQuery" name="q" value="<%= query %>" required>
<button type="submit">Search</button>
</form>
<% if (results.length > 0) { %>
<h2>Search Results</h2>
<ul>
<% results.forEach(result => { %>
<li>
<h3><%= result.title %></h3>
<p><%= result.content %></p>
</li>
<% }); %>
</ul>
<% } else if (query) { %>
<p>No results found for "<%= query %>".</p>
<% } %>
</body>
</html>
Step 7: Create Sample Data
In data/sample-data.json
, create sample data for indexing and searching:
[
{
"title": "Introduction to Node.js",
"content": "Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.",
"tags": ["Node.js", "JavaScript"]
},
{
"title": "Express.js Basics",
"content": "Express.js is a minimal and flexible Node.js web application framework.",
"tags": ["Express.js", "Node.js"]
},
{
"title": "Web Development with HTML and CSS",
"content": "Learn the basics of building web pages using HTML and CSS.",
"tags": ["HTML", "CSS", "Web Development"]
}
]
Step 8: Run Your Application
In server.js
, use the exported app
to start the application:
const app = require('./src/app');
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Start the server:
node server.js
Visit http://localhost:3000/search in your browser to see and interact with your search engine. Enter a search query in the form and click the "Search" button to see the search results.
Congratulations! You've successfully created a basic search engine using Node.js, Express, and the Lunr library for indexing and searching. Customize and expand this application based on your specific requirements.