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

node-week2/nada #351

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions 6_nodejs/week2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
77 changes: 77 additions & 0 deletions 6_nodejs/week2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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());

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

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});

// Load documents from documents.json
let documents = [];
fs.readFile('documents.json', 'utf8', (err, data) => {
if (err) {
console.error("Error reading documents:", err);
return;
}
documents = JSON.parse(data);
});

// GET /search
app.get("/search", (req, res) => {
const query = req.query.q;
const fields = req.body.fields;

if (query && fields) {
res.status(400).send("Both query parameter and fields in body cannot be provided simultaneously.");
} else if (query) {
const matchedDocuments = documents.filter(doc => Object.values(doc).some(value => typeof value === 'string' && value.includes(query)));
res.json(matchedDocuments);
} else {
res.json(documents);
}
});

// GET /documents/:id
app.get("/documents/:id", (req, res) => {
const id = parseInt(req.params.id);
const document = documents.find(doc => doc.id === id);
if (document) {
res.json(document);
} else {
res.status(404).send("Document not found");
}
});

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

if (query && fields) {
res.status(400).send("Both query parameter and fields in body cannot be provided simultaneously.");
} else if (query) {
const matchedDocuments = documents.filter(doc => Object.values(doc).some(value => typeof value === 'string' && value.includes(query)));
res.json(matchedDocuments);
} else if (fields) {
const filteredDocuments = documents.filter(doc => {
for (const field in fields) {
if (doc[field] !== fields[field]) {
return false;
}
}
return true;
});
res.json(filteredDocuments);
} else {
res.json(documents);
}
});
13 changes: 13 additions & 0 deletions 6_nodejs/week2/documents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"id": 1,
"name": "Used Apple Airpods",
"price": "50",
"description": "Battery life is not great"
},
{
"id": 2,
"type": "doc",
"value": "hello world"
}
]
Loading
Loading