-
Notifications
You must be signed in to change notification settings - Fork 1
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
Server Files #1
base: main
Are you sure you want to change the base?
Server Files #1
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
const exp = require("constants"); | ||
const cookieParser = require("cookie-parser"); | ||
const express = require("express"); | ||
const { request} = require("http"); | ||
const { type } = require("os"); | ||
const app = express(); | ||
|
||
const PORT = 8080; | ||
|
||
app.use(express.json()); | ||
app.use(express.urlencoded({extended:false})); | ||
app.use(express.text()); | ||
app.use(cookieParser()); | ||
|
||
const USERNAME = "Shurahbeel"; | ||
const PASSWORD = "password"; | ||
|
||
function specialwords(data) { | ||
const notallowed = "!@#$%^&*()_+[]{}|;':\",./<>?`~\\-="; | ||
for (let i = 0; i < data.length; i++) { | ||
if (notallowed.includes(data[i])) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
const repo_data = [ | ||
{ | ||
"repo_name": "Hackathon prep", | ||
"details": "this repo comprises code regarding hackathon 2024", | ||
"repo_author": "Shurahbeel Peerzada", | ||
"commits": 214, | ||
"year_created": 2024, | ||
}, | ||
{ | ||
"repo_name": "Machine Learning Models", | ||
"details": "This repository contains machine learning models and datasets.", | ||
"repo_author": "Alexis Jordan", | ||
"commits": 150, | ||
"year_created": 2023 | ||
}, | ||
{ | ||
"repo_name": "Weather App", | ||
"details": "A weather forecasting application using React and Node.js.", | ||
"repo_author": "Liam Patterson", | ||
"commits": 75, | ||
"year_created": 2022 | ||
}, | ||
{ | ||
"repo_name": "E-commerce Backend", | ||
"details": "Backend code for an e-commerce platform built with Django.", | ||
"repo_author": "Emily Martinez", | ||
"commits": 340, | ||
"year_created": 2021 | ||
}, | ||
{ | ||
"repo_name": "Portfolio Website", | ||
"details": "Personal portfolio website showcasing projects and blogs.", | ||
"repo_author": "Shurahbeel Peerzada", | ||
"commits": 90, | ||
"year_created": 2024 | ||
}, | ||
{ | ||
"repo_name": "Data Visualization Tool", | ||
"details": "A tool for visualizing complex data sets using D3.js.", | ||
"repo_author": "Nina West", | ||
"commits": 125, | ||
"year_created": 2023 | ||
} | ||
] | ||
|
||
app.listen(PORT, () => console.log(`Running on port number: ${PORT}`)); | ||
|
||
app.post('/login', (req, res) => { | ||
const { username, password } = req.body; | ||
if (username === USERNAME && password === PASSWORD) { | ||
res.cookie('auth', 'loggedin', { httpOnly: true }); | ||
res.status(201).send("User Successfully Logged in"); | ||
} else { | ||
res.status(404).send("No user found with these credentials"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. explore http repsonse codes and use a better response code here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated with better code |
||
} | ||
}); | ||
|
||
app.get('/getallrepos', (req, res) => { | ||
if (req.cookies.auth === "loggedin") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. explore express middlewares and use an auth middleware instead of checking of the cookie for each route. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the auth middleware wherever necessary |
||
res.status(200).send(repo_data); | ||
} else { | ||
res.status(401).send("Unauthorized Access. Please Login"); | ||
} | ||
}); | ||
|
||
app.get('/getrepodetail', (req, res) => { | ||
if (req.cookies.auth === 'loggedin') { | ||
let name = req.query.name; | ||
if (name) { | ||
if (specialwords(name)) { | ||
res.status(400).send("Please avoid using special characters"); | ||
} else { | ||
Comment on lines
+125
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets move this to middleware as well. In this middleware you can do |
||
let specificRepo = repo_data.find(repo => repo.repo_name === name); | ||
if (specificRepo === undefined) { | ||
res.status(404).send("No repo matching the provided name exists"); | ||
} else { | ||
res.status(200).send(specificRepo); | ||
} | ||
} | ||
} else { | ||
res.status(400).send("Please provide a repo name"); | ||
} | ||
} else { | ||
res.status(401).send("Unauthorized Access. Please Login"); | ||
} | ||
}); | ||
|
||
app.post('/create-repo', (req, res) => { | ||
if (req.cookies.auth === 'loggedin') { | ||
const data = req.body; | ||
|
||
if (specialwords(data.repo_name) || specialwords(data.repo_author) || specialwords(data.details)) { | ||
res.status(400).send("Please avoid special characters in Name, Author, and Details"); | ||
} else if (typeof data.year_created !== "number" || typeof data.commits !== "number") { | ||
res.status(400).send("Please use numbers when providing commits and year created"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create an express middleware to handle validation logic and use the middleware chaining. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made the changes by including a repovalidation middleware function and chained it to the create-repo route |
||
} else { | ||
repo_data.push(data); | ||
res.status(201).send("Repo Created"); | ||
} | ||
} else { | ||
res.status(401).send("Unauthorized Access. Please Login"); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if username and password are provided. If not return 400 response with message to let user know of the missing fields
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the required changes