Skip to content
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: 1 addition & 1 deletion client/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VITE_API_URL=http://localhost:5005
VITE_API_URL=http://localhost:5005/api
150 changes: 150 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"jsonwebtoken": "^9.0.3"
}
}
1 change: 0 additions & 1 deletion server/.env.sample

This file was deleted.

46 changes: 21 additions & 25 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
const express = require("express");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const PORT = 5005;

// STATIC DATA
// Devs Team - Import the provided files with JSON data of students and cohorts here:
// ...
try {
process.loadEnvFile(__dirname + "/.env");
} catch (error) {
console.warn(".env file not found, using default environment values");
}

const express = require("express");
const User = require("./models/user.models.js");

// INITIALIZE EXPRESS APP - https://expressjs.com/en/4x/api.html#express
const app = express();
const config = require("./config");
config(app);

const connectDB = require("./db");
app.use(async (req, res, next) => {
await connectDB();
next();
});

// MIDDLEWARE
// Research Team - Set up CORS middleware here:
// ...
app.use(express.json());
app.use(morgan("dev"));
app.use(express.static("public"));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
const indexRouter = require("./routes/index.routes.js");
app.use("/api", indexRouter);

const errorHandling = require("./errors");
errorHandling(app);

// ROUTES - https://expressjs.com/en/starter/basic-routing.html
// Devs Team - Start working on the routes here:
// ...
app.get("/docs", (req, res) => {
res.sendFile(__dirname + "/views/docs.html");
});


// START SERVER
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
app.listen(process.env.PORT, () => {
console.log(`Server listening on port ${process.env.PORT}`);
});
57 changes: 0 additions & 57 deletions server/cohorts.json

This file was deleted.

20 changes: 20 additions & 0 deletions server/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");

function config(app) {
app.use(
cors({
origin: process.env.ORIGIN,
}),
);

app.use(express.json());
app.use(morgan("dev"));
app.use(express.static("public"));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
}

module.exports = config;
14 changes: 14 additions & 0 deletions server/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mongoose = require("mongoose");

async function connectDB() {
if (mongoose.connection.readyState === 1) return;
try {
const response = await mongoose.connect(process.env.MONGODB_URI);
const dbName = response.connections[0].name;
console.log(`Connected to Mongo! Database name: "${dbName}"`);
} catch (err) {
console.error("Error connecting to mongo: ", err);
}
}

module.exports = connectDB;
22 changes: 22 additions & 0 deletions server/errors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function errorHandling(app) {



//error handling for 404 errors
app.use((req, res) => {
res.status(404).json({ errorMessage: "Sorry, route not found" });
});

// error handling for 500 errors
app.use((error, req, res, next) => {
// express know this is the 500 error handler just because it has 4 parameters.
console.log(error);
res
.status(500)
.json({ errorMessage: "something went BOOM, sorry about this" });
});

}


module.exports = errorHandling
19 changes: 19 additions & 0 deletions server/middlewares/auth.middlewares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const jwt = require("jsonwebtoken");

function verifyToken(req, res, next) {
try {
console.log(req.headers);

const authToken = req.headers.authorization.split(" ")[1];

const payload = jwt.verify(authToken, process.env.TOKEN_SECRET);

req.payload = payload;

next();
} catch (error) {
res.status(401).json({ errorMessage: "Token not provided or not valid" });
}
}

module.exports = verifyToken;
19 changes: 19 additions & 0 deletions server/models/cohort.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const mongoose = require("mongoose")
const Schema = mongoose.Schema

const cohortSchema = new Schema({
inProgress: Boolean,
cohortSlug: String,
cohortName: String,
program: String,
campus: String,
startDate: String,
endDate: String,
programManager: String,
leadTeacher: String,
totalHours: Number

})
const Cohort =mongoose.model("Cohort", cohortSchema)
module.exports = Cohort

Loading