This project is a RESTful API built with Node.js, Express, and Sequelize, allowing users to manage students and campuses.
- Campus Management:
- Add, edit, delete, and fetch campuses.
- Fetch campuses with associated students.
- Student Management:
- Add, edit, delete, and fetch students.
- Fetch students with their associated campus.
- Middleware: CORS, body-parser
bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install nodenpm -v- Clone the repository:
git clone https://github.com/AleHS01/campus-student-api.git
cd campus-student-api- Install dependecies:
npm install- Setup
.envfile:
- Create a
.envfile in the root directory. - Add your database credentials and server configuration:
PORT=your_port
POSTGRES_URL=your_db_url- Start the server
npm run start- or use a task runner like nodemon for automatic restarts:
nodemon index.js- Local Backend URL:
http://localhost:8080- Campus Endpoints
- GET
/api/campus: Fetch all campuses. - GET
/api/campus/:id: Fetch a campus by ID (with associated students). - POST
/api/campus/addCampus: Add a new campus. - PUT
/api/campus/updateCampus/:id: Update a campus by ID. - DELETE
/api/campus/removeCampus/:id: Delete a campus by ID.
- GET
- Student Endpoints
- GET
/api/student: Fetch all students. - GET
/api/student/:id: Fetch a student by ID (with associated campus). - POST
/api/student/addStudent: Add a new student. - PUT
/api/student/updateStudent/:id: Update a student by ID. - DELETE
/api/student/removeStudent/:id: Delete a student by ID.
- GET
This project uses Sequelize to define models and manage relationships with a PostgreSQL database. Below is an overview of the database structure and relationships between the models.
The Campus model represents a campus in the system and contains the following fields:
id: The primary key, auto-incremented integer.name: The name of the campus (required).imageurl: URL for the campus image, with a default value of an empty string.address: The address of the campus (required).description: A textual description of the campus.
const { DataTypes } = require("sequelize");
const db = require("../db");
const Campus = db.define("Campus", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
imageurl: {
type: DataTypes.STRING,
defaultValue: "",
},
address: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.STRING,
},
});
module.exports = Campus;The Student model represents a student enrolled in a campus and contains the following fields:
id: The primary key, auto-incremented integer.firstname: The student's first name (required).lastname: The student's last name (required).email: The student's email address (required, must be a valid email).imageurl: URL for the student's image, with a default value.gpa: The student's GPA (must be between 0.0 and 4.0).CampusId: A foreign key linking the student to a campus. This is an optional relationship, as students may not be associated with a campus initially.
const { DataTypes } = require("sequelize");
const db = require("../db");
const Student = db.define("Student", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
firstname: {
type: DataTypes.STRING,
allowNull: false,
},
lastname: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isEmail: {
msg: "Invalid Email Format",
},
},
},
imageurl: {
type: DataTypes.STRING,
defaultValue: "https://picsum.photos/200",
},
gpa: {
type: DataTypes.DECIMAL,
validate: {
min: 0.0,
max: 4.0,
}
},
CampusId: {
type: DataTypes.INTEGER,
allowNull: true
}
});
module.exports = Student;- One-to-Many Relationship:
- A Campus can have many Students. This relationship is represented by the
CampusIdforeign key in theStudentmodel. - Sequelize handles the relationship by associating each
Studentwith aCampus.
- A Campus can have many Students. This relationship is represented by the
Campus.hasMany(Student);
Student.belongsTo(Campus);- The database connection is managed by Sequelize, and the connection details are stored in the environment variable
POSTGRES_URL. - The connection is established using
sequelize.authenticate(), which logs a success message to the console if the connection is successful.
const { Sequelize } = require("sequelize");
const dotenv = require("dotenv").config();
const pg = require("pg");
const db = new Sequelize(process.env.POSTGRES_URL + "?sslmode=require", {
logging: false,
dialectModule: pg,
});
db.authenticate()
.then(() => {
console.log("DB connection works");
})
.catch((error) => {
console.error("DB connection failed:", error);
});
module.exports = db;- Sequelize models and associations are defined in database/models/index.js.
- Database connection is established in database/db.js.
- The seed script can populate the database with initial data by enabling this on
index.js:await seed()
- All API routes are located in the
api/folder and accessible throughtapi/index.js:const router = require("express").Router(); router.use("/student", require("./student")); router.use("/campus", require("./campus")); router.use((req, res, next) => { const error = new Error("404 Not Found"); error.status = 404; next(error); }); module.exports = router;
This project is deployed using Vercel, though it is not currently active. You can deploy your own instance by following these steps:
- Visit Vercel and create an account.
- Link your GitHub repository to Vercel.
- Set up deployment settings and deploy the project.