Skip to content

Commit

Permalink
adding cors
Browse files Browse the repository at this point in the history
  • Loading branch information
LordFarquaadtheCreator committed Jun 4, 2024
1 parent 1e0ca22 commit ac67428
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 106 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@types/pg": "^8.11.6",
"chai": "^5.1.1",
"chai-http": "^4.4.0",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"nodemon": "^3.1.1",
Expand Down
206 changes: 100 additions & 106 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,147 +2,141 @@ import express from "express";
import activateDb from "./db";
import { readFile } from "fs";
import winston from "winston";
import cors from "cors";

// activateDb();
const app = express();
const PORT = 3000;

const logger = winston.createLogger({
// Log only if level is less than (meaning more severe) or equal to this
level: "info",
// Use timestamp and printf to create a standard log format
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`
)
// Log only if level is less than (meaning more severe) or equal to this
level: "info",
// Use timestamp and printf to create a standard log format
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`,
),
// Log to the console and a file
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: "logs/app.log" }),
],
),
// Log to the console and a file
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: "logs/app.log" }),
],
});

app.use(cors());

app.use((req, res, next) => {
// Log an info message for each incoming request
logger.info(`Received a ${req.method} request for ${req.url}`);
next();
// Log an info message for each incoming request
logger.info(`Received a ${req.method} request for ${req.url}`);
next();
});

app.get("/", (req, res) => {
res.send("BYTE @ CCNY").status(200);
res.send("BYTE @ CCNY").status(200);
});

app.get("/projects", (req, res) => {
if (req.query) {
logger.warn("Query parameters ignored");
}
if (req.query) {
logger.warn("Query parameters ignored");
}

readFile("./data.json", "utf8", (error, content) => {
if (error) {
logger.error("Error reading data.json");
return res.status(500).send("Error reading file");
}
return res.status(200).json(JSON.parse(content));
});
readFile("./data.json", "utf8", (error, content) => {
if (error) {
logger.error("Error reading data.json");
return res.status(500).send("Error reading file");
}
return res.status(200).json(JSON.parse(content));
});
});

app.get("/projects/team", (req, res) => {
if (!req.query.team) {
logger.error("Team query parameter missing");
res.status(400).send("Missing team");
return;
}
if (!req.query.team) {
logger.error("Team query parameter missing");
res.status(400).send("Missing team");
return;
}

readFile("./data.json", "utf8", (error, content) => {
if (error) {
logger.error("Error reading data.json");
return res.status(500).send("Error reading file");
}
const jsonData = JSON.parse(content);
const filteredData = jsonData.filter((item: any) => {
const itemData = item.team.toString().toLowerCase().split(",");
const queryData = req.query.team
?.toString()
.toLowerCase()
.split(",");
return queryData?.every((query) => itemData.includes(query));
});
if (filteredData.length === 0) {
logger.warn("No projects found");
return res
.status(404)
.send("The data you are looking for does not exist");
}
return res.status(200).send(filteredData);
readFile("./data.json", "utf8", (error, content) => {
if (error) {
logger.error("Error reading data.json");
return res.status(500).send("Error reading file");
}
const jsonData = JSON.parse(content);
const filteredData = jsonData.filter((item: any) => {
const itemData = item.team.toString().toLowerCase().split(",");
const queryData = req.query.team?.toString().toLowerCase().split(",");
return queryData?.every((query) => itemData.includes(query));
});
if (filteredData.length === 0) {
logger.warn("No projects found");
return res
.status(404)
.send("The data you are looking for does not exist");
}
return res.status(200).send(filteredData);
});
});

app.get("/projects/cohort", (req, res) => {
if (!req.query.cohort) {
logger.error("Cohort query parameter missing");
res.send("Missing cohort").status(400);
return;
}
if (!req.query.cohort) {
logger.error("Cohort query parameter missing");
res.send("Missing cohort").status(400);
return;
}

readFile("data.json", "utf8", (err, data) => {
if (err) {
logger.error("Error reading data.json");
res.send("Error reading file").status(500);
}
const jsonData = JSON.parse(data);
const filteredData = jsonData.filter((item: any) => {
const itemData = item.cohort.toString().toLowerCase().split(",");
const queryData = req.query.cohort
?.toString()
.toLowerCase()
.split(",");
console.log(itemData, queryData);
return itemData.some((item: any) => queryData?.includes(item));
});

if (filteredData.length === 0) {
logger.warn("No projects found");
res.send("No projects found").status(404);
return;
}
res.send(filteredData).status(200);
readFile("data.json", "utf8", (err, data) => {
if (err) {
logger.error("Error reading data.json");
res.send("Error reading file").status(500);
}
const jsonData = JSON.parse(data);
const filteredData = jsonData.filter((item: any) => {
const itemData = item.cohort.toString().toLowerCase().split(",");
const queryData = req.query.cohort?.toString().toLowerCase().split(",");
console.log(itemData, queryData);
return itemData.some((item: any) => queryData?.includes(item));
});

if (filteredData.length === 0) {
logger.warn("No projects found");
res.send("No projects found").status(404);
return;
}
res.send(filteredData).status(200);
});
});

app.get("/projects/name", (req, res) => {
if (!req.query.name) {
logger.error("Name query parameter missing");
res.send("Missing project name").status(400);
return;
}
if (!req.query.name) {
logger.error("Name query parameter missing");
res.send("Missing project name").status(400);
return;
}

readFile("data.json", "utf8", (err, data) => {
if (err) {
logger.error("Error reading data.json");
res.send("Error reading file").status(500);
}
const jsonData = JSON.parse(data);
const filteredData = jsonData.filter((item: any) => {
const itemData = item.name.toString().toLowerCase();
const queryData = req.query.name
?.toString()
.toLowerCase()
.split(",");
return queryData?.some((query) => itemData.includes(query));
});
if (filteredData.length === 0) {
logger.warn("No projects found");
res.send("No projects found").status(404);
return;
}
res.send(filteredData).status(200);
readFile("data.json", "utf8", (err, data) => {
if (err) {
logger.error("Error reading data.json");
res.send("Error reading file").status(500);
}
const jsonData = JSON.parse(data);
const filteredData = jsonData.filter((item: any) => {
const itemData = item.name.toString().toLowerCase();
const queryData = req.query.name?.toString().toLowerCase().split(",");
return queryData?.some((query) => itemData.includes(query));
});
if (filteredData.length === 0) {
logger.warn("No projects found");
res.send("No projects found").status(404);
return;
}
res.send(filteredData).status(200);
});
});

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

export default app;

0 comments on commit ac67428

Please sign in to comment.