In this project for the Treehouse techdegree, I created a REST API using Express. The API provides a way for users to administer a school database containing information about courses: users can interact with the database by retrieving a list of courses, as well as adding, updating and deleting courses in the database.
In addition, users are required to create an account and log-in to make changes to the database.
- Node.js
- Express
- Sequelize ORM
- SQL
- REST API design
- JavaScript
- Download or clone from Github
- Run
npm install - Run
npm run seed - Run
npm run startto run the app
The environment variable RESTAPI_DB must be set to the SQLITE database ./fsjstd-restapi.db. This is already configured in nodemon.json for nodemon setups.
- Seeds the SQLITE database
- Runs app via nodemon
- Runs app via nodemon in inspect mode for debugging in Visual Studio Code or Google Chrome
GET /api/users 200- Returns the currently authenticated userPOST /api/users 201- Creates a user, sets the Location header to "/", and returns no content
GET /api/courses 200- Returns a list of courses (including the user that owns each course)GET /api/courses/:id 200- Returns the course (including the user that owns the course) for the provided course IDPOST /api/courses 201- Creates a course, sets the Location header to the URI for the course, and returns no content (Authentication required)PUT /api/courses/:id 204- Updates a course and returns no content (Authentication required)DELETE /api/courses/:id 204- Deletes a course and returns no content (Authentication required)- The
PUT /api/courses/:idandDELETE /api/courses/:idroutes return a 403 status code if the current user doesn't own the requested course
- The following properties are filtered out:
password,createdAt,updatedAt - If a course or a route isn't found a 404 status is returned and no content
- The RESTAPI.postman_collection.json file is a collection of Postman requests that can be used to test the REST API.
- The api.http file serves the same purpose and can be used with the Visual Studio Code REST Client extension. It uses variables, so it can also be used to test a deployed version.
- The gen-data.js tool in
./helperscan be used to generate test data.
- Error messages are always returned in an array of error objects
- User:
firstName,lastName,emailAddress,passwordare required - Course:
title,description,userIdare required emailAddressmust be unique and a valid email addresspasswordmust be at least 8 characters long
- Validation errors are routed through from Sequelize and return the properties:
message,type,path,value. This enables a client application to create their own error messages if required.
Example:
{
"errors": [
{
"message": "User.password cannot be null",
"type": "notNull Violation",
"path": "password",
"value": null
}
]
}- Other errors return the properties:
message,type
Example:
{
"errors": [
{
"message": "Unexpected token f in JSON at position 5",
"type": "SyntaxError"
}
]
}