- Building RESTful APIs.
- Performing CRUD Operations on Multiple Resources.
- Configuring CORS.
- Writing Custom Middleware.
- Using Express Routers to Modularize Application.
Use Node.js and Express to design and build an API that performs CRUD operations on users, posts and post tags. Write custom middleware to ensure that the tags are uppercased before they are processed by the request handlers.
- Fork and Clone this repository.
- CD into the folder where you cloned the repository.
- Do your magic!
- Take the steps necessary to create a
package.json
to keep a record of our dependencies. - use yarn or npm to add knex and sqlite3 as dependencies to the project. This is required for database access.
- Configure an npm script named "start" that will execute your code using nodemon so that the server restarts on changes. Make nodemon be a development time dependency only, it shouldn't be deployed to production.
- Design and build a set of endpoints that satisfy the API requirements.
- Use Postman to test the API as you work through the exercises.
The /data/helpers
folder includes helper files that you can use to manage the persistence of users, posts and tags data. These files are userDb.js
, postDb.js
and tagDb.js
. All three of them publish the following api, that you can use to store, modify and retrieve each resource:
get()
: calling find returns a promise that resolves to an array of all the resources contained in the database. If you pass anid
to this method it will return the resource with that id if found.insert()
: calling insert passing it a resource object will add it to the database and return an object with the id of the inserted resource. The object looks like this:{ id: 123 }
.update()
: accepts two arguments, the first is theid
of the resource to update and the second is an object with thechanges
to apply. It returns the count of updated records. If the count is 1 it means the record was updated correctly.remove()
: the remove method accepts anid
as it's first parameter and, upon successfully deleting the resource from the database, returns the number of records deleted.
The postDb.js
helper includes an extra method called getPostTags()
that when passed a post id as it's only argument, returns a list of all the tags for the post.
The userDb.js
helper includes an extra method called getUserPosts()
that when passed a user id as it's only argument, returns a list of all the posts for the user.
All these helper methods return a promise.
The schemas (properties and data type of each property) used to store and retrieve the resources inside the included database (lambda.sqlite3
) is described below.
- id: number, no need to provide it when creating users, the database will generate it.
- name: up to 128 characters long, required.
- id: number, no need to provide it when creating posts, the database will automatically generate it.
- userId: number, required, must be the id of an existing user.
- text: string, no size limit, required.
- id: number, no need to provide it when creating tags, the database will generate it.
- tag: string up to 80 characters long, must be a unique value.
We have provided test data for all the resources.
Now that we have a way to add, update, remove and retrieve data from the provided database, it is time to work on the API.
Design and build the necessary endpoints to:
- perform CRUD operations on users, posts, and tags.
- retrieve the list of posts for a user.
- retrieve the list of tags for a post.
- Use
create-react-app
to create an application inside the root folder, name itclient
. - From the React application connect to the
/api/users
endpoint 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/:id
route to show the user details. - Add styling!