-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e35280f
Showing
17 changed files
with
4,113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.