Welcome to the Backend Assessment!
This assessment is designed to help you practice the skills and knowledge you have acquired from the WD-107 Backend course. You will be building a Node.js
Express
MongoDB
application to make API endpoints and combine data from the Rick and Morty API. You will create simple and complex GET
routes, as well as full CRUD
functionality for a character favorites
collection. You will also need to connect your app to a MongoDB
database, and create a full login system with Authentication using JWT
.
To complete this assessment, complete every task in Parts I through VII outlined below.
Requirements to Pass
- All routes must return the correct status code and data.
- Routes, controllers, models, middleware, etc. must be created in the appropriate files and folders.
- In
Part I
throughIII
,async/await
andtry/catch
blocks must be used to make requests and handle errors rather than promise chaining. - Errors must be handled and returned to the user.
- The
db.js
file must be set up correctly. - The
MongoDB
database must be set up correctly. - The login system must be secured using
JWT
. - Your code must be clean and readable. It should follow the DRY principle.
- Routes created in Part VI should be accessed only by users who are logged in.
- Error handling middleware is implemented for all
favorites
routes (created in Part VI). - Auth middleware is implemented for all
favorites
routes (created in Part VI).
Important Note(s)
- Use axios to make requests to the Rick and Morty API.
- Save all sensitive passwords and keys to an
.env
file. - Upon completing this assessment, submit your Github repo link and the
.env
file.
Documentation
- Postman documentation
- axios documentation
- Express documentation
- Rick and Morty API
- MongoDB
- Mongoose
- JWT
- bcryptjs
- jsonwebtoken
- dotenv
Downloads
- Download Compass to view your MongoDB database.
- Download Postman to test your API endpoints.
Set up your project by following the instructions below.
- Fork and clone this repository.
- Run
npm install
to install dependencies. - Run
npm run server
to start the server w/nodemon
. - Create a
.gitignore
file and addnode_modules
and.env
to it. - Create a
.env
file and addPORT=3000
to it. - Set up the
app.js
file:- Require the
express
module - Set up the
app
object - Set the
port
to3000
- Set up the
app
object to useexpress.json()
middleware to parse JSON bodies - Add a
GET
route to/
that returns a200
status and a JSON object with amessage
key assigned to a string that reads,"Welcome to the Rick and Morty API!"
- Set up the server to listen on port
3000
. - Run your server with
npm run server
- Check that your server is running:
- You should see your
"Listening on port 3000"
log in the terminal. - When you visit
http://localhost:3000/
in your browser, you should see a JSON object,{message: "Welcome to the Rick and Morty API!"}
.
- You should see your
- Require the
Commit to Github
- Commit your work to Github and push to your forked repository w/ a commit message that reads,
"Part I - Setup"
.
Create the following GET
routes in the characterRoutes.js
file:
- Use
/api/characters
as the base path for all character routes
/api/characters
Route
Create a GET
route in the characterRoutes.js
file that:
- Returns an array of Rick and Morty characters.
- If the
catch
block is triggered, returns a500
status and a JSON object with amessage
key assigned to a string that reads,"Server error. Please try again later."
/api/characters/:id
Route
Create a GET
route in the characterRoutes.js
file that:
- Returns a single Rick and Morty character object based on an
id
parameter. - If the
catch
block is triggered, it returns the errorstatus
and a JSON object with amessage
key assigned to the errorstatusText
. Use Axios Response Schema documentation as needed.
/api/characters/status/:status
Route
Create a GET
route in the characterRoutes.js
file that:
- Returns an array of Rick and Morty characters who are either alive or dead based on a
status
parameter.- The array of characters should be an array of name strings, sorted by first name in alphabetical order.
- If the
status
parameter is notalive
ordead
, it returns a404
status and a JSON object with amessage
key assigned to a string that reads,"The status must be alive or dead."
- If the
catch
block is triggered, it returns the errorstatus
and a JSON object with amessage
key assigned to the error message contained in theaxios
error response data. Use Axios Response Schema documentation as needed.
Commit to Github
- Commit your work to Github and push to your forked repository w/ a commit message that reads,
"Part II - API GET Routes: Return Rick and Morty Data"
.
/api/characters/location/:location
Route
Create a GET
route in the characterRoutes.js
file that:
- Returns an array of Rick and Morty character names who are residents of the first returned location. If multiple locations match the parameter, only return the characters from the first location in the returned array.
- If the
catch
block is triggered, it returns the errorstatus
and a JSON object with amessage
key assigned to the errorstatusText
. Use Axios Response Schema documentation as needed.
Commit to Github
- Commit your work to Github and push to your forked repository w/ a commit message that reads,
"Part III - Advanced Routes: Return Combined Data"
.
- Set up a
MongoDB
database - Connect it to your application.
- Add the
MONGO_URI
to your.env
file
Create Authentication using JWT
:
-
Create a
User
model that has the following properties:-
username
- a string -
password
- a string -
email
- a string - Set all properties to required
- Make sure that the
email
is unique - Include a timestamp
-
-
Create a
register
route where users can sign up for your application- All passwords should be hashed using
bcryptjs
and 10 salt rounds - Make sure that the user's
email
is unique - If the user's
email
is not unique, return a400
status and a JSON object with amessage
key assigned to a string that reads,"Email already exists."
- All passwords should be hashed using
-
Create a
login
route where users can sign in-
login
route should compare the hashed password to the password in the user request and returns aJWT token
-
-
Create a
logout
route that invalidates theJWT token
-
Create the
JWT token
usingjsonwebtoken
-
Create a middleware that checks for the
JWT token
and verifies it -
Create a private
/me
route that returns the user's information based on theJWT token
-
Store the
JWT_SECRET
in your.env
file
Commit to Github
- Commit your work to Github and push to your forked repository w/ a commit message that reads,
"Part VI - Authentication"
.
Note: All functionality in this section should only be accessed by authenticated users.
- Implement
asyncHandler
to avoidtry/catch
blocks and to trigger error handling middleware (error middleware to be completed in Part VII) - Create a new collection in your database called
favorites
- Create a
model
for yourfavorites
collection in thedb.js
file with a schema that includes the following:-
user
- the user'sid
and a reference to theUser
model -
name
- the character's name -
image
- the character's image URL
-
- Create a route that allows a user to add a character's
name
andimage
url as well as adescription
of why they added the character to theirfavorites
collection - Create a route that allows a user to
get
all of theirfavorites
- Create a route that allows a user to
remove
a character from theirfavorites
- Create a route that allows a user to
edit
theirdescription
of why they added the character to their list offavorites
Commit to Github
- Commit your work to Github and push to your repository w/ a commit message that reads,
"Part VII - Collection of Favorite Characters"
Create an errorHandler
function in the errorMiddleware.js
file that:
- Responds with a
statusCode
and errormessage
- Returns the
stack
trace only if the environment is indevelopment
- Implement the
errorHandler
function in yourapp.js
file - Check that the
errorHandler
is working with all of thefavorites
routes created in Part VI. No need to implement forcharacters
routes.
Commit to Github
- Commit your work to Github and push to your repository w/ a commit message that reads,
"Part VII - Error Handling Middleware"
- Ensure all routes work as expected
- Ensure that your code is readable and DRY
- Include the
.env
file