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

react2-week1/srushti #346

Open
wants to merge 4 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
11 changes: 11 additions & 0 deletions 6_nodejs/week1/avg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Using array destructuring method through spread syntax, the 3 commas will skip the first 3 elemnts (node, script path and operation)
from the command line. */

const [, , , ...numbers] = process.argv;

if (numbers.length === 0 || numbers.some(isNaN)) {
console.error('Error: Please provide valid numbers as command line arguments.');
} else {
const average = numbers.reduce((acc, num) => acc + Number(num), 0) / numbers.length;
console.log('Average:', average);
}
12 changes: 12 additions & 0 deletions 6_nodejs/week1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "week1",
"version": "1.0.0",
"description": "",
"main": "avg.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
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/
96 changes: 96 additions & 0 deletions 6_nodejs/week2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const express = require("express");
const fs = require("fs");
const app = express();
const port = process.env.PORT || 3000;

app.use(express.json());

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

if (!query) {
return res.status(400).json({ error: "No query provided" });
}

fs.readFile('./documents.json', 'utf8', (err, data) => {
if (err) {
console.error("Error reading JSON file:", err);
return res.status(500).json({ error: "Error reading JSON file" });
}

try {
const documents = JSON.parse(data);

const filteredDocuments = documents.filter((doc) => {
return Object.values(doc).some((value) =>
value && value.toString().toLowerCase().includes(query.toLowerCase())
);
});

res.json(filteredDocuments);
} catch (error) {
console.error("Error parsing JSON data:", error);
res.status(500).json({ error: "Error parsing JSON data" });
}
});
});

app.get("/documents/:id", (req, res) => {
const id = parseInt(req.params.id);

try {
const data = fs.readFileSync("./documents.json", "utf8");
const documents = JSON.parse(data);

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

if (foundDocument) {
res.json(foundDocument);
} else {
res.status(404).send("Error 404: Document not found");
}
} catch (error) {
console.error("Error reading JSON file:", error);
res.status(500).send("Error reading JSON file");
}
});

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

if (query && fields) {
res.status(400).json({ error: '"query" and "fields" cannot be provided' });
return;
}

try {
const data = fs.readFileSync("./documents.json", "utf8");
const documents = JSON.parse(data);

let filteredDocuments = documents;

if (query) {
filteredDocuments = filteredDocuments.filter((doc) =>
Object.values(doc).some((value) => value.includes(query))
);
}

if (fields) {
filteredDocuments = filteredDocuments.filter((doc) =>
Object.entries(fields).every(
([key, value]) => doc[key] && doc[key].toString() === value.toString()
)
);
}

res.json(filteredDocuments);
} catch (error) {
console.error("Error reading JSON file:", error);
res.status(500).json({ error: "Error reading JSON file" });
}
});

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
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