In this challenge, you build an API and write custom middleware that satisfies the requirements listed under the Minimum Viable Product section.
There are two possible ways to submit your project. Your instructor should have communicated which method to use for this project during the Guided Project and in your cohort's Slack channel. If you are still unsure, reach out to Lambda Staff.
- Fork and clone the repository.
- Open the assignment in Canvas and click on the "Set up git" option.
- Follow instructions to set up Codegrade's Webhook and Deploy Key.
- Push your first commit:
git commit --allow-empty -m "first commit" && git push. - Check to see that Codegrade has accepted your git submssion.
- Fork and clone the repository.
- Implement your project in a
firstname-lastnamebranch. - Create a pull request of
firstname-lastnameagainst yourmainbranch. - Open the assignment in Canvas and submit your pull request.
- Wire the application together completing
api/server.jsandindex.js. - Write four custom middleware functions detailed below, in
api/middleware/middleware.js. - Use the custom middlewares in their appropriate places in the application (specific endpoints, entire routes or globally).
- There are endpoints in
users-router.jsto retrieve the list ofpostsby auserand to store a newpostfor auser.
-
logger()loggerlogs to the console the following information about each request: request method, request url, and a timestamp- this middleware runs on every request made to the API
-
validateUserId()- this middleware will be used for all user endpoints that include an
idparameter in the url (ex:/api/users/:idand it should check the database to make sure there is a user with that id. - if the
idparameter is valid, store the user object asreq.userand allow the request to continue - if the
idparameter does not match any user id in the database, respond with status404and{ message: "user not found" }
- this middleware will be used for all user endpoints that include an
-
validateUser()validateUservalidates thebodyon a request to create or update a user- if the request
bodylacks the requirednamefield, respond with status400and{ message: "missing required name field" }
-
validatePost()validatePostvalidates thebodyon a request to create a new post- if the request
bodylacks the requiredtextfield, respond with status400and{ message: "missing required text field" }
There are two helper files that you can use to manage the persistence of users and posts data. These files are api/users/users-model.js and api/posts/posts-model.js. Both files publish the following api:
get(): calling find returns a promise that resolves to an array of all the resources contained in the database.getById(): takes anidas the argument and returns a promise that resolves to the resource with that id if found.insert(): calling insert passing it a resource object will add it to the database and return the new resource.update(): accepts two arguments, the first is theidof the resource to update and the second is an object with thechangesto apply. On success it returns the updated record.remove(): the remove method accepts anidas it's first parameter and, upon successfully deleting theresourcefrom the database, returns the number of records deleted.
The users-model.js includes an extra method called getUserPosts() that when passed a user's id, returns a list of all the posts for the user.
All helper methods return a promise.
The Database Schemas for the users and posts resources are:
| field | data type | metadata |
|---|---|---|
| id | unsigned integer | primary key, auto-increments, generated by database |
| name | string | required, unique |
| field | data type | metadata |
|---|---|---|
| id | unsigned integer | primary key, auto-increments, generated by database |
| text | text | required |
| user_id | unsigned integer | required, must be the id of an existing user |
We have provided test data for the resources.
- Run tests locally executing
npm test. - Reset the database by executing
npm run resetdb. - You are welcome to create additional files but do not move or rename existing files or folders.
- Do not alter your
package.jsonfile except to install additional libraries or add additional scripts. - In your solution, it is essential that you follow best practices and produce clean and professional results.
- Schedule time to review, refine, and assess your work.
- Perform basic professional polishing including spell-checking and grammar-checking on your work.
- Create a React App
- Use
create-react-appto create an application inside the root folder, name itclient. - From the React application connect to the
/api/usersendpoint in the API and show the list of users. - Add functionality to show the details of a user, including their posts, when clicking a user name in the list. Use React Router to navigate to a
/users/:idroute to show the user details. - Add styling!
- Use