This week was YelpCamp: Initial Routes and Databases from Colt Steele The Web Developer Bootcamp.
-YelpCamp: Initial Routes
-YelpCamp: Layout
-YelpCamp: Creating Campgrounds
-What is a Database
-Mongo Shell Basics
-Introduction to Mongoose
YelpCamp: Initial Routes
YelpCamp will have a landing page and a campground page that lists all campgrounds.
Each campground will have a name and an image.
There is an easier way to install multiple packages at once.
npm install express ejs –save
The landing page will be on the root app
Var express = require(“express”);
Var app = express();
App.set(“View engine”, “ejs”);
App.get(“/”, function(req, res) {
Res.render(“landing”);
})
})
Until we start the database, the data will be held in an array.
This is an example
[
{name: “Camp Ground”, image: http://www.image.com},
{name: “Camp Ground”, image: http://www.image.com} ,
{name: “Camp Ground”, image: http://www.image.com}
]
The array is sufficient to hold data for the time being.
App.get(“/campgrounds”, function(req, res) {
Var campgrounds = [
{name: “Salmon Creek”, image: “” } ,
{name: “Granite Hill”, image: “” } ,
{name: “Mountain Goat”, image: “” }
]
Res.render(“campgrounds”);
})
App.listen(process.env.PORT, process.env.IP, function() {
Console.log(“The YelpCamp Server Has Started!”);
YelpCamp: Layout
Create our header and footer partials as well as add in Bootstrap for styling.
<html>
<head>
<title>YelpCamp</title>
</head>
The header is placed in it one partial that separates it from the body. The body is placed in its own partial as well.
<body>
</body>
</html>
Partials are used to define the header and the footer which make routing across pages more seamless.
YelpCamp: Creating Campgrounds
-Creating new campgrounds
-Setup new campground POST route
This is the post route
```app.post(“/campgrounds”, function(req, res) {
var name = req.body.name
var image = req.body.image
var newCampground = {name: name, image: image}
campgrounds.push(newCampground);
res.redirect(“/campgrounds”);
});
A form will send a post request to somewhere then inside that post route we take the form data, we do something with it, then we redirect back to somewhere else.
The feature will be added that will allow a user to submit a new campground.
To do this, we must first setup the post route that we create with the new campground added. Then we will need to add in the body parser and make sure that everything is properly configured. Then we will need to create the form and then create the route for that form. A user will be able to send that POST request.
What is a Database
If the server were to stop, we would lose all the data.
A database is a collection of information/data. Databases have an interface for interacting with the stored data. Interaction with data may consist of adding a new user to the database or removing a user from a database. Editing a user information within that database.
An interface is just a way to write code that interacts with that data within the database.
SQL (relational database) vs. NoSQL (non-relational database)
SQL databases are tabular in that they are flat.
Tabular means that you must define a table ahead of time before the data can be utilized. It is not very flexible.
You must define what a user looks like with
ID | Name | Age | City
| --------- |:---------:| -------:|
| Customer | Ruby | 30 | New York
All users must have this pattern of id/name/age/city.
JOIN TABLES are what tie relationships between data together.
A join table can tie a user id with their comment id together.
NoSQL (non-relational database)
Tables are not used, data can be nested. It is not a flat database. It looks similar to JSON but the data is actually written in BJSON which is binary JavaScript Object Notation. But really it is just JSON with a bunch of key-value pairs.
{
Name: “June”,
Age: 25,
City: New York,
Comments: [
{text: “New York is Awesome”}
{text: “New York is The best”}
]
}
Comments can be nested inside of the data, IDs are not needed, tables do not need to be defined ahead of time. It is more flexible.
Mongo Shell Basics
mongod
Mongo
Help
Show dbs
Use
Insert
Find
Update
remove
Introduction to Mongoose
Mongoose is known as an ODM which is an object data mapper. It is a way for us to write JavaScript inside our JavaScript files.
Mongoose makes it possible to use schemes within our data. A schema is a way to organize our data in a way that makes since to our project.
https://mongoosejs.com/
Top comments (0)