Group Deliverable Task: Building an Optimized API with Express, Mongoose, and User Authentication
Objectives
- Create an API using Express.js and Mongoose that follows the MVC architecture.
- Implement user authentication in your API.
- Serve some static files using
express.static
. - Write tests for your API using Jest and Supertest.
- Analyze your API's Latency & Throughput and find ways to optimize it.
- Document your findings and the steps you took to optimize your API's performance.
- Fix any bugs or issues that come up when you run your tests.
Instructions
- Set up your project: Create a new Node.js project and install the necessary dependencies:
Express
,Mongoose
,bcryptjs
,jsonwebtoken
,Jest
,Supertest
,morgan
, and any other packages you think you'll need. - Design your API: Your API should have at least the following endpoints:
GET /users
,POST /users
,PUT /users/:id
,DELETE /users/:id
,POST /users/login
, andPOST /users/logout
. Feel free to add more endpoints if you'd like. - Follow the MVC architecture: Structure your project following the Model-View-Controller (MVC) architecture.
- Implement user authentication: Use JWT (JSON Web Tokens) for authentication. Make sure the
PUT
,DELETE
,POST /users/login
, andPOST /users/logout
routes require authentication. - Serve static files: Add a route in your API that serves static files using
express.static
. This could be animages
directory with some pictures, apublic
directory with an HTML file and a CSS file, or something similar. - Write tests for your API: Create a test file for your user routes and use Jest and Supertest to write tests for each endpoint. Remember to test both success and failure cases.
- Analyze and optimize your API's performance: Use
morgan
or another middleware to log the Latency & Throughput of your API. Try to find ways to optimize its performance, and document your findings and the steps you took. - Fix any issues: Run your tests and fix any bugs or issues that come up. Make sure all your tests pass before you submit your project.
Submission
Commit your source code, tests, and a brief report (as a PDF or Markdown file) to a GitHub repository. Your report should include:
- An overview of your API.
- A description of how you implemented user authentication.
- An explanation of how you structured your project following the MVC architecture.
- A summary of your findings when analyzing and optimizing your API's performance.
- Any challenges you faced during the project and how you overcame them.
Submit the GitHub repository link via Canvas.
Evaluation Criteria
- Proper implementation of the MVC architecture.
- Correct use of Express.js and Mongoose to build an API.
- Successful implementation of user authentication.
- Serving static files using
express.static
. - Clear and thorough tests for your API using Jest and Supertest.
- Detailed analysis and optimization of your API's Latency & Throughput.
- Well-structured, clear, and concise report.
- All tests should pass.
Static Files
Create a static files folder for CSS/JS
- CSS/JS code doesn't change with server-side data
-
We can toss any static files into a 'public' directory
- static means unchanging
- dynamic means changing depending on data
Let's set up a directory for our static code:
- Create a directory called
public
- Inside the
public
directory create a directory calledcss
- Inside the
css
directory, create anapp.css
file - Put some CSS in the
app.css
file - Inside server.js place the following near the top:
CSS
@import url('https://fonts.googleapis.com/css?family=Comfortaa|Righteous');
body {
background: url(https://images.clipartlogo.com/files/istock/previews/8741/87414357-apple-seamless-pastel-colors-pattern-fruits-texture-background.jpg);
margin: 0;
font-family: 'Comfortaa', cursive;
}
h1 {
font-family: 'Righteous', cursive;
background: antiquewhite;
margin:0;
margin-bottom: .5em;
padding: 1em;
text-align: center;
}
a {
color: orange;
text-decoration: none;
text-shadow: 1px 1px 1px black;
font-size: 1.5em;
background: rgba(193, 235, 187, .9);
}
a:hover {
color: ghostwhite;
}
li {
list-style: none;
}
li a {
color: mediumseagreen;
}
input[type=text] {
padding: .3em;
}
input[type=submit] {
padding: .3em;
color: orange;
background: mediumseagreen;
font-size: 1em;
border-radius: 10%;
}
Add to server.js or app.js depending on how you arranged your application
app.use(express.static('public')); //tells express to try to match requests with files in the directory called 'public'
You then could access this file by going to http://localhost:3000/app.css
if your server is running