This documentation provides an overview of the codebase for a Node.js application that serves as a basic API for user authentication and managing blog posts. The application is built using Express.js and utilizes TypeORM for database interaction. Below, you'll find details on the main components of the codebase, including server setup, routing, controllers, and middleware.
Before setting up the project, ensure that you have the following prerequisites installed on your development environment:
- Node.js and npm: Download and install Node.js and npm from nodejs.org.
- PostgreSQL: Install PostgreSQL, a relational database system, and set up a database for the project. You'll need the database connection details.
Follow these steps to set up the project:
-
Clone the Repository:
git clone https://github.com/oluwasemilorebadejo/blog-api.git
-
Navigate to the Project Directory:
cd blog-api
-
Install Dependencies:
npm install
-
Environment Variables:
- Create a
.env
file in the project root directory to store environment variables. You can use the providedconfig.env
as a template. - Configure the following environment variables in your
.env
file:NODE_ENV
: Set to "development" for development environment.PORT
: Port number for the server.DATABASE_HOST
: PostgreSQL database connection URL.DATABASE_PORT
: 5432DATABASE_USERNAME
: PostgreSQL database connection URL.DATABASE_PASSWORD
: PostgreSQL database connection URL.DATABASE_NAME
: PostgreSQL database connection URL.
- Create a
-
Database Setup:
- Ensure PostgreSQL is running.
- Create a PostgreSQL database that matches the
DATABASE_URL
in your.env
file.
-
TypeORM Configuration:
- Open
ormconfig.json
in the project root directory. - Update the
url
property with your PostgreSQL database connection URL.
- Open
-
Run
npm start
to run the project.
The server.ts
file is the entry point for the application. It sets up the server, connects to the database, and handles uncaught exceptions.
- Uncaught Exception Handling: Any unhandled exceptions are caught and logged before the application exits.
- Environment Variables: Loads environment variables from a
config.env
file using thedotenv
package. - Database Connection: Connects to the database using TypeORM.
The app.ts
file sets up the Express application, configures middleware, and defines routing for the API.
- Logging: If the environment is set to "development," HTTP request logging is enabled using the
morgan
middleware. - JSON Parsing: The application uses
express.json()
to parse JSON request bodies. - Cookie Parsing: The
cookie-parser
middleware is used to parse cookies.
- Authentication Routes: Routes related to user authentication are defined in the
auth.route.ts
file and mounted under the/api/v1/auth
path. - Post Routes: Routes for managing blog posts are defined in the
post.route.ts
file and mounted under the/api/v1/posts
path. - Error Handling: A catch-all route handler sends a 404 error for undefined routes.
Global error handling middleware (globalErrorHandler
) is used to catch and format errors consistently throughout the application.
The auth.route.ts
file defines routes related to user authentication, including user signup and login.
- POST
/signup
: Allows users to register with their email, username, and password. - POST
/login
: Handles user login using email and password credentials.
The post.route.ts
file defines routes for managing blog posts, including creating, updating, deleting, and retrieving posts. These routes are protected and require authentication.
- Authentication Middleware: The
protect
middleware is used to ensure that only authenticated users can access these routes.
- GET
/
: Retrieves a list of blog posts. Supports pagination, filtering, sorting, and field selection via query parameters. - POST
/
: Creates a new blog post. - GET
/:id
: Retrieves a specific blog post by ID. - PATCH
/:id
: Updates a specific blog post by ID. - DELETE
/:id
: Deletes a specific blog post by ID.
The auth.controller.ts
file contains controller functions for user authentication, including signup and login.
signup
: Handles user registration by checking for existing users, password matching, and creating and sending a token upon successful registration.login
: Handles user login by verifying user credentials and sending a token upon successful login.
The post.controller.ts
file contains controller functions for managing blog posts.
addPost
: Creates a new blog post associated with the authenticated user.updatePost
: Updates an existing blog post based on the provided ID.getPost
: Retrieves a specific blog post by ID.deletePost
: Deletes a specific blog post by ID.getAllPosts
: Retrieves a list of blog posts with support for pagination, filtering, sorting, and field selection.