Skip to content
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

[PT RMT WD OCT2022] Zefanja Timorason #120

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
Binary file added .DS_Store
Binary file not shown.
Binary file added assets/.DS_Store
Binary file not shown.
23 changes: 23 additions & 0 deletions backend/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Dependency directories
node_modules/

# Debug log from npm
npm-debug.log

# Environment Variables should NEVER be published
.env

# macOS
.DS_Store
.AppleDouble
.LSOverride

# Windows
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
*.lnk


# VS Code
.vscode/*
27 changes: 27 additions & 0 deletions backend/backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require("dotenv").config();

// ℹ️ Connects to the database
require("./db");

// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require("express");

const app = express();

// ℹ️ This function is getting exported from the config folder. It runs most pieces of middleware
require("./config")(app);

// 👇 Start handling routes here
const indexRoutes = require("./routes/index.routes");
app.use("/api", indexRoutes);

const phoneRoutes = require("./routes/phones.routes");
app.use("/api", phoneRoutes);

// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require("./error-handling")(app);

module.exports = app;
38 changes: 38 additions & 0 deletions backend/backend/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// We reuse this import in order to have access to the `body` property in requests
const express = require("express");

// ℹ️ Responsible for the messages you see in the terminal as requests are coming in
// https://www.npmjs.com/package/morgan
const logger = require("morgan");

// ℹ️ Needed when we deal with cookies (we will when dealing with authentication)
// https://www.npmjs.com/package/cookie-parser
const cookieParser = require("cookie-parser");

// ℹ️ Needed to accept from requests from 'the outside'. CORS stands for cross origin resource sharing
// unless the request if from the same domain, by default express wont accept POST requests
const cors = require("cors");

const FRONTEND_URL = process.env.ORIGIN || "http://localhost:3000";

// Middleware configuration
module.exports = (app) => {
// Because this is a server that will accept requests from outside and it will be hosted ona server with a `proxy`, express needs to know that it should trust that setting.
// Services like heroku use something called a proxy and you need to add this to your server
app.set("trust proxy", 1);

// controls a very specific header to pass headers from the frontend
app.use(
cors({
origin: [FRONTEND_URL]
})
);

// In development environment the app logs
app.use(logger("dev"));

// To have access to `body` property in the request
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
};
19 changes: 19 additions & 0 deletions backend/backend/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ℹ️ package responsible to make the connection with mongodb
// https://www.npmjs.com/package/mongoose
const mongoose = require("mongoose");

// ℹ️ Sets the MongoDB URI for our app to have access to it.
// If no env has been set, we dynamically set it to whatever the folder name was upon the creation of the app

const MONGO_URI =
process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/backend";

mongoose
.connect(MONGO_URI)
.then((x) => {
const dbName = x.connections[0].name;
console.log(`Connected to Mongo! Database name: "${dbName}"`);
})
.catch((err) => {
console.error("Error connecting to mongo: ", err);
});
21 changes: 21 additions & 0 deletions backend/backend/error-handling/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = (app) => {
app.use((req, res, next) => {
// this middleware runs whenever requested page is not available
res.status(404).json({ message: "This route does not exist" });
});

app.use((err, req, res, next) => {
// whenever you call next(err), this middleware will handle the error
// always logs the error
console.error("ERROR", req.method, req.path, err);

// only render if the error ocurred before sending the response
if (!res.headersSent) {
res
.status(500)
.json({
message: "Internal server error. Check the server console",
});
}
});
};
38 changes: 38 additions & 0 deletions backend/backend/models/Phone.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { Schema, model } = require("mongoose");

const phoneSchema = new Schema({
id: {
type: Number,
},
name: {
type: String,
},
manufacturer: {
type: String,
},
description: {
type: String,
},
color: {
type: String,
},
price: {
type: Number,
},
imageFileName: {
type: String,
},
screen: {
type: String,
},
processor: {
type: String,
},
ram: {
type: Number,
},
});

const Phones = model("Phones", phoneSchema);

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

// TODO: Please make sure you edit the User model to whatever makes sense in this case
const userSchema = new Schema(
{
email: {
type: String,
required: [true, 'Email is required.'],
unique: true,
lowercase: true,
trim: true
},
password: {
type: String,
required: [true, 'Password is required.']
}
},
{
// this second object adds extra properties: `createdAt` and `updatedAt`
timestamps: true
}
);

const User = model("User", userSchema);

module.exports = User;
Loading