Commit 84fcfe95 authored by Kristian Freeman's avatar Kristian Freeman

mongodb-express exercise

parent 5011e343
// Import the file ./utils/connection, setting
// it to the variable connect.
// Import body-parser, setting it to bodyParser.
// Import express.
// Import mongoose.
// Import morgan.
// Set the variable port to process.env.PORT or 8080.
// Set the variable app to the result of calling the express function.
// Call app.use, passing in the morgan function, which you should call
// with the string argument "combined". This sets up a nice logger
// middleware for express apps.
// Call app.use, passing in the function bodyParser.urlEncoded, which
// takes a single object as an argument, with the key extended
// and value false. This allows express to parse body data from an
// HTML form.
// Call app.use, passing in the function bodyParser.json() as the argument.
// Require './routes/user', destructuring the named export userRouter
// from inside of it.
// Call app.use, which takes two arguments: "/users", and userRouter.
// This sets up a "sub-router" for our express application, which nests everything
// inside of the /users section of our app.
// Set the view engine of express to pug.
// Define a root route "/" that sends a status of 200 back to the user.
// Define an async function "init", which calls:
// - the connect function (use await)
// - (optionally) drops the database whenever the app is started,
// using mongoose.connection.dropDatabase() (use await for this)
// - app.listen, passing in the port and a callback function which logs
// the port our express app is listening on
// Call the init function.
// Require mongoose and set it to the variable mongoose.
// Define userSchema, which is the value of calling mongoose.Schema,
// passing in an object where:
// - id is a Number
// - name is a String
// - description is a String
// - created_at is an object with type Date and default Date.now
// - updated_at is an object with type Date and default Date.now
// Define the variable User, which is the response of calling
// mongoose.model with two arguments: the string "User" and userSchema.
// Next, we'll define some helper functions used to assist lookups
// in our Mongo collection.
// These functions will all build off of the User model, with the
// end-goal of having each function be "chainable" via promises.
// Note that any **query** in mongoose does _not_ return a promise:
// for these, we'll have to chain .exec() on the end of it to
// explicitly return a promise.
//
// e.g. Todo.find().exec() <- exec() chained on the end
// getUsers is a function that calls User.find(). Chain .exec() on the end of this
// function.
// getUser is a function with an argument id, that returns the result of calling
// the User.findOne, passing in an object with a key and value of id. Chain
// .exec() on the end of this function.
// createUser is a function with an argument body, that returns the result of
// creating a new instance of the User class, passing in the body argument.
// Chain .save() to the end of this new User call.
// updateUser is a function that takes two arguments, id and body.
// It returns the function User.findOneAndUpdate, passing three arguments:
// - an object with key and value id
// - body
// - an _options_ object with new set to true. This indicates that
// mongoose should return the _new_ version of this updated object
// Chain .exec() to the end of this function
// deleteUser is a function that takes an argument id, and calls User.deleteOne,
// passing an object with key and value id.
// Finally, export each of our helper functions from this file:
// - getUsers
// - getUser
// - createUser
// - updateUser
// - deleteUser
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "mongodb-express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.3.12",
"morgan": "^1.9.1",
"pug": "^2.0.3"
},
"devDependencies": {
"nodemon": "^1.18.6"
}
}
// In this exercise, we'll implement the user "subroutes" to support
// a simple HTML interface for our MongoDB User collection.
//
// Specifically, we'll define the following (note that all routes are
// _relative_ to this router, meaning they all begin with "/users":
// - /, which should get all the users from Mongo, and render a
// Pug template "users/index", passing in the users as data to
// the template, in the format { users: users }
//
// - /new, which should render a Pug template "users/new"
//
// - /:id, which gets the id param from the request, calls getUser,
// and renders the Pug template "users/show", passing in the data
// { user: user }
//
// - /, which accepts a request body, calls createUser with that body,
// and redirects to /users/:id of the new user
// To start, let's import a couple things.
// First, we need to import express - specifically, the "Router" function
// from inside of request. Either require express as a variable, and then
// set the variable Router to express.Router, or just destructure Router
// out of the express require call.
// Next, define userRouter, which is the response of calling the Router
// function.
// Next, import the following named functions from the user file in the
// models directory:
// - getUsers
// - getUser
// - createUser
// Now we can define our first route. Call userRouter.get, passing in the
// path "/", and defining an **async** callback function.
// This function should set the variable users to the value of getUsers
// (using await to wait for the async method getUsers to finish),
// and then should render the template "users/index", passing in a data
// object where users is set to the key users.
// Our next route is another get request, this time to "/new".
// It should render the template "users/new".
// Our next route is a get request at "/:id", with another async callback
// function.
// This function sets id to the value of params.id inside of our request.
// It also defines the variable user, which is the value of calling getUser
// with id (make sure to use await). Render the template "users/show", passing
// in a data object with a key and value of user.
// Our next route is a post to the root route, "/". The callback function
// (async) function sets body from of request.body. It then calls createUser,
// passing the body variable into the function (use await). Set this variable
// to user. Once the user has been created, we should redirect to the user
// route, using res.redirect. Pass in the url /users/:id, where id is the
// new user id.
// Finally, set module.exports to an object, passing in userRouter
// (so userRouter is a _named_ export of this file).
// Make sure that you also look at the files inside the views folder,
// which have some Pug functionality for you to implement so you can actually
// view your users in-browser.
{
"id": 1,
"name": "Kristian Freeman"
}
const mongoose = require("mongoose");
const port = process.env.PORT || 27017;
const url = `mongodb://localhost:${port}/training-mongodb-express`;
const connect = () =>
mongoose.connect(
url,
{ useNewUrlParser: true }
);
module.exports = connect;
//- For each user in the users array in data,
//- render an h1 tag that has a value of user.name
//- Define a link tag with an ahref of /users/new, and the text
//- "Create a new user"
//- Define a form tag with the following attributes
//- - action is "/users"
//- - method is "POST"
//- Inside of the form, add four divs.
//- The first div has a label with an attribute "for" set to "id",
//- and text "ID".
//- It also has an input with type="text" and name="id"
//- The second div has a label with an attribute "for" set to "name",
//- and text "name".
//- It also has an input with type="text" and name="name"
//- The third div has a label with an attribute "for" set to
//- "description", and text "description".
//- It also has an input with type="text" and name="description"
//- The fourth div has a button with type="submit", and the text "Create user"
//- Define an h1 with the text #{user.name}
//- Define an h2 with the text "Created #{user.created_at}"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment