Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 2 Warmup #224

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions nodejs/week2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
class30-homework/
89 changes: 89 additions & 0 deletions nodejs/week2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import express from 'express';
import fs from 'fs';

const app = express();
const port = process.env.PORT || 3000;

// Support parsing JSON requests
app.use(express.json());

// The documents.json file is assumed to be in the same directory as app.js
const documentsFilePath = './documents.json';

// Helper function to load documents from the JSON file
const loadDocuments = () => {
const data = fs.readFileSync(documentsFilePath);
return JSON.parse(data);
};

app.get("/", (req, res) => {
res.send("This is a search engine");
});

// GET /search endpoint
app.get("/search", (req, res) => {
const q = req.query.q; // Retrieve the query parameter q
const documents = loadDocuments();

if (q) {
// Filter documents where any field contains the query
const result = documents.filter(doc =>
Object.values(doc).some(value =>
String(value).toLowerCase().includes(q.toLowerCase())
)
);
return res.json(result);
}

// If q is not provided, return all documents
res.json(documents);
});

// GET /documents/:id endpoint
app.get("/documents/:id", (req, res) => {
const { id } = req.params;
const documents = loadDocuments();

const document = documents.find(doc => doc.id == id);

if (!document) {
return res.status(404).send("Document not found");
}

res.json(document);
});

// POST /search endpoint
app.post("/search", (req, res) => {
const { q } = req.query;
const { fields } = req.body;

if (q && fields) {
return res.status(400).send("Both query parameter q and fields in body can't be provided at the same time.");
}

const documents = loadDocuments();

if (q) {
const result = documents.filter(doc =>
Object.values(doc).some(value =>
String(value).toLowerCase().includes(q.toLowerCase())
)
);
return res.json(result);
}

if (fields) {
const result = documents.filter(doc =>
Object.keys(fields).every(field => doc[field] === fields[field])
);
return res.json(result);
}

res.status(400).send("Please provide either q query parameter or fields in the request body.");
});

// Start the server
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
14 changes: 14 additions & 0 deletions nodejs/week2/documents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"id": 1,
"name": "Used Apple Airpods",
"price": "50",
"description": "Battery life is not great"
},
{
"id": 2,
"type": "doc",
"value": "hello world"
}
]

Loading