Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghalia98 committed Feb 14, 2022
0 parents commit e35280f
Show file tree
Hide file tree
Showing 17 changed files with 4,113 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dependency directory
node_modules

# Debug log from npm
npm-debug.log

# Environment Variables should NEVER be published
.env
34 changes: 34 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ℹ️ 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");

// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require("hbs");

const app = express();

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

// default value for title local
const projectName = "recipe-app";
const capitalized = (string) => string[0].toUpperCase() + string.slice(1).toLowerCase();

app.locals.title = `${capitalized(projectName)} created with IronLauncher`;

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

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

module.exports = app;
39 changes: 39 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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");

// ℹ️ Serves a custom favicon on each request
// https://www.npmjs.com/package/serve-favicon
const favicon = require("serve-favicon");

// ℹ️ global package used to `normalize` paths amongst different operating systems
// https://www.npmjs.com/package/path
const path = require("path");

// Middleware configuration
module.exports = (app) => {
// 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());

// Normalizes the path to the views folder
app.set("views", path.join(__dirname, "..", "views"));
// Sets the view engine to handlebars
app.set("view engine", "hbs");
// Handles access to the public folder
app.use(express.static(path.join(__dirname, "..", "public")));

// Handles access to the favicon
app.use(favicon(path.join(__dirname, "..", "public", "images", "favicon.ico")));
};
19 changes: 19 additions & 0 deletions 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://localhost/recipe-app";

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

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).render("error");
}
});
};
20 changes: 20 additions & 0 deletions models/User.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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(
{
username: {
type: String,
// unique: true -> Ideally, should be unique, but its up to you
},
password: String,
},
{
// this second object adds extra properties: `createdAt` and `updatedAt`
timestamps: true,
}
);

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

module.exports = User;
Loading

0 comments on commit e35280f

Please sign in to comment.