Skip to content

Commit

Permalink
chore: Create search.ejs routed to /search and search bar in header
Browse files Browse the repository at this point in the history
-
  • Loading branch information
Kaillr committed Oct 18, 2024
1 parent 349c162 commit d202467
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
13 changes: 13 additions & 0 deletions backend/routes/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require("express");
const router = express.Router();

// Route for the search page
router.get("/", (req, res) => {
res.render("search", {
title: "Search - Minishop",
});
});

// Export the router
module.exports = router;

10 changes: 9 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const db = require('./db/db');
const indexRoutes = require('./routes/index');
const registerRoutes = require('./routes/register');
const loginRoutes = require('./routes/login');
const searchRoutes = require('./routes/search');

const app = express();
const port = process.env.PORT || 3000;
Expand All @@ -24,10 +25,17 @@ app.use(express.urlencoded({ extended: true }));
// Serve static files from the frontend directory
app.use(express.static(path.join(__dirname, '../frontend')));

// Middleware to set query variable globally
app.use((req, res, next) => {
res.locals.query = req.query.query || ""; // Set query globally for all views
next();
});

// Define routes
app.use("/", indexRoutes);
app.use("/register", registerRoutes);
app.use("/login", loginRoutes)
app.use("/login", loginRoutes);
app.use("/search", searchRoutes);

// Catch-all 404 handler for unavailable routes
app.use((req, res, next) => {
Expand Down
11 changes: 11 additions & 0 deletions frontend/views/pages/search.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<%- include("../partials/head") %>
</head>
<body>
<%- include("../partials/header") %>

<%- include("../partials/footer") %>
</body>
</html>
5 changes: 4 additions & 1 deletion frontend/views/partials/header.ejs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<header>
<h1><a href="/">Minishop</a></h1>
<form action="/search" method="get" class="search">
<input type="text" name="query" placeholder="Search..." value="<%= query %>">
<button type="submit">Search</button>
</form>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/login">Login</a></li>
<li><a href="/register">Register</a></li>
</ul>
Expand Down

0 comments on commit d202467

Please sign in to comment.