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
1 change: 0 additions & 1 deletion server/.env.sample

This file was deleted.

39 changes: 33 additions & 6 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
require("dotenv/config");


const express = require("express");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const PORT = 5005;
const cors = require("cors")
const mongoose = require("mongoose")
const { errorHandler, notFoundHandler } = require("./error-handling/index.js")


mongoose
.connect('mongodb://127.0.0.1:27017/cohorts-tools-api')
.then(x => console.log(`Connected to Mongo! Database name: "${x.connections[0].name}"`))
.catch(err => console.error('Error connecting to mongo', err));

// STATIC DATA
// Devs Team - Import the provided files with JSON data of students and cohorts here:
// ...

const cohorts = require("./cohorts.json")
const students = require("./students.json")

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


// MIDDLEWARE
// Research Team - Set up CORS middleware here:
// ...
app.use(cors( {
origin: ["http://localhost:5173"]
} ));
app.use(express.json());
app.use(morgan("dev"));
app.use(express.static("public"));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(errorHandler);


// 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");
});

const studentsRouter = require('./routes/students.routes.js')
app.use("/api/students", studentsRouter)

const cohortsRouter = require('./routes/cohorts.routes.js')
app.use("/api/cohorts", cohortsRouter)

const authRouter = require("./routes/auth.routes.js");
app.use("/auth", authRouter)

const usersRouter = require("./routes/user.routes.js");
app.use("/api/users", usersRouter);

//Not Found Error Handler at the end of all routes!
app.use(notFoundHandler);


// START SERVER
app.listen(PORT, () => {
Expand Down
17 changes: 17 additions & 0 deletions server/error-handling/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function errorHandler (err, req, res, next) {

console.error("ERROR", req.method, req.path, err);

if(!res.headersSent) {

res.status(500).json({ message: "Internal server error. Check the server console."});

}
}

function notFoundHandler (req, res, next) {

res.status(404).json({message: "This route does not exist"})
}

module.exports = { errorHandler, notFoundHandler}
16 changes: 16 additions & 0 deletions server/middleware/jwt.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const jwt = require("jsonwebtoken");

const isAuthenticated = (req, res, next) => {
try{
const token = req.headers.authorization.split(" ")[1];
//console.log("Authorization header:", req.headers.authorization)
const payload = jwt.verify(token, process.env.TOKEN_SECRET);
req.payload = payload;

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

module.exports = { isAuthenticated }
20 changes: 20 additions & 0 deletions server/models/Cohort.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require("mongoose")
const Schema = mongoose.Schema


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

const Cohort = mongoose.model("Cohort", cohortSchema);

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

const studentSchema = new Schema({
firstName: String,
lastName: String,
email: String,
phone: String,
linkedUrl: String,
languages: [String],
program: String,
background: String,
image: String,
projects: [String],
cohort: {
type: mongoose.Schema.Types.ObjectId,
ref: "Cohort"
}
})

const Student = mongoose.model("Student", studentSchema)

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

const userSchema = new Schema({
email: {
type: String,
unique: true
},
password: String,
name: String,


});

module.exports = model("User", userSchema)

Loading