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
6 changes: 6 additions & 0 deletions package-lock.json

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

48 changes: 25 additions & 23 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
const express = require("express");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const PORT = 5005;
try {
process.loadEnvFile()
} catch (error) {
console.log("can't access to .env file")
}

// STATIC DATA
// Devs Team - Import the provided files with JSON data of students and cohorts here:
// ...
const mongoose = require("./db")
const express = require("express");

const Cohort = require("./models/Cohort.model");
const Student = require("./models/Student.model");

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

// Midlleware Routes:
const config = require("./config")
config(app)

// 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());


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

// Routes of Collections:
const indexRouter = require("./routes/index.routes")
app.use("/api", indexRouter)

// Error Handlers:
const errorHandlers = require("./error-handlers")
errorHandlers(app)

// Start Server:
const PORT = process.env.PORT || 5005

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

This file was deleted.

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

function config (app){

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());

}

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

mongoose
.connect("mongodb://127.0.0.1:27017/cohort-tools-api") // 127.0.0.1 same as localhost
.then(() => {
console.log("data base connected");
})
.catch((error) => {
console.log(error);
});

module.exports = mongoose
12 changes: 12 additions & 0 deletions server/error-handlers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function errorHandlers(app){

app.use((req, res) => {
res.status(404).json({message: "Could't find anything, maybe you've lost." })
})

app.use((error, req, res, next) => {
res.status(500).json({errorMessage: "Something went wrong."})
})
}

module.exports = errorHandlers
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: {type: Boolean, default: false},
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
24 changes: 24 additions & 0 deletions server/models/Student.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require("mongoose")

const Schema = mongoose.Schema

const studentSchema = new Schema({
firstName: String,
lastName: String,
email: String,
phone: String,
linkedinUrl: 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
Loading