Make sure you have PostgreSQL installed and running. Then, create a database and tables:
CREATE DATABASE blog;
CREATE TABLE Users (
id SERIAL PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL
);
CREATE TABLE Blogs (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE User_Like_Blog (
user_id INTEGER REFERENCES users(id),
blog_id INTEGER REFERENCES blogs(id),
PRIMARY KEY (user_id, blog_id)
);
Modify db.js
file in config
directory
npm install
npm start
By following these steps, you should be able to use Postman to register a user, log in to get a JWT token, and then use that token to post a blog and like a blog post.
To post a blog or like a blog using Postman, you'll need to follow these steps. Here’s a step-by-step guide:
- Open Postman.
- Create a new POST request to
http://localhost:3000/auth/register
. - In the Body tab, select
raw
and chooseJSON
format. - Enter the JSON data for the new user. For example:
{ "username": "testuser", "password": "password123" }
- Send the request. You should get a response with the user details.
- Create a new POST request to
http://localhost:3000/auth/login
. - In the Body tab, select
raw
and chooseJSON
format. - Enter the JSON data for the login credentials. For example:
{ "username": "testuser", "password": "password123" }
- Send the request. You should get a response with a JWT token. Copy this token.
- Create a new POST request to
http://localhost:3000/blog
. - In the Headers tab, add a new header:
- Key:
Authorization
- Value:
Bearer <your_jwt_token>
- Replace
<your_jwt_token>
with the token you copied from the login response.
- Key:
- In the Body tab, select
raw
and chooseJSON
format. - Enter the JSON data for the blog post. For example:
{ "title": "My First Blog Post", "content": "This is the content of my first blog post." }
- Send the request. You should get a response with the details of the created blog post.
- Create a new POST request to
http://localhost:3000/blog/<blog_id>/like
.- Replace
<blog_id>
with the ID of the blog post you want to like.
- Replace
- In the Headers tab, add a new header:
- Key:
Authorization
- Value:
Bearer <your_jwt_token>
- Replace
<your_jwt_token>
with the token you copied from the login response.
- Key:
- Send the request. You should get a response indicating that the blog post was liked.
- Create a new GET request to
http://localhost:3000/blog
.
When you add the Authorization
header in Postman, it should look something like this:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
-
Register User:
- URL:
http://localhost:3000/auth/register
- Method: POST
- Body:
{ "username": "testuser", "password": "password123" }
- URL:
-
Login User:
- URL:
http://localhost:3000/auth/login
- Method: POST
- Body:
{ "username": "testuser", "password": "password123" }
- URL:
-
Post Blog:
- URL:
http://localhost:3000/blog
- Method: POST
- Headers:
- Key:
Authorization
- Value:
Bearer <your_jwt_token>
- Key:
- Body:
{ "title": "My First Blog Post", "content": "This is the content of my first blog post." }
- URL:
-
Like Blog Post:
- URL:
http://localhost:3000/blog/<blog_id>/like
- Method: POST
- Headers:
- Key:
Authorization
- Value:
Bearer <your_jwt_token>
- Key:
- URL:
-
List All Blog Posts:
- URL:
http://localhost:3000/blog
- Method: GET
- URL:
project-root|
├── config/
│ └── db.js
├── controllers/
│ └── authController.js
│ └── blogController.js
├── models/
│ └── user.js
│ └── blog.js
├── routes/
│ └── authRoutes.js
│ └── blogRoutes.js
├── middlewares/
│ └── authMiddleware.js
├── views/
│ └── index.ejs
├── public/
│ ├── css/
│ └── js/
├── app.js
└── package.json
- Purpose: Contains configuration files for your application.
- Example File:
db.js
- This file sets up and exports the database connection so that other parts of your application can use it to interact with the database.
- Purpose: Contains the logic for handling requests and responses. This is where the main actions of your application occur.
- Example Files:
authController.js
: Handles user registration and login actions.blogController.js
: Handles actions related to creating blog posts, liking posts, and listing posts.
- Purpose: Represents the data structure of your application and interacts with the database. Each model corresponds to a table in the database.
- Example Files:
user.js
: Defines the User model, including methods for creating users and finding users by username.blog.js
: Defines the Blog model, including methods for creating blog posts, liking posts, and listing posts.
- Purpose: Defines the endpoints (routes) of your application and maps them to the appropriate controller actions.
- Example Files:
authRoutes.js
: Defines routes related to user authentication, such as registration and login.blogRoutes.js
: Defines routes related to blog posts, such as creating, liking, and listing posts.
- Purpose: Contains middleware functions that perform tasks before the main request handler is executed. These can be used for things like authentication and logging.
- Example File:
authMiddleware.js
: Contains a function to verify the JWT token, ensuring that users are authenticated before accessing certain routes.
- Purpose: Contains templates for rendering the HTML views of your application. This is more relevant for server-side rendering.
- Example File:
index.ejs
: A template file for the main page of your application (if you were rendering HTML views).
- Purpose: Contains static files like CSS and JavaScript that are served directly to the client.
- Example Subfolders:
css/
: Directory for CSS files.js/
: Directory for JavaScript files.
app.js
: The main entry point of your application. It sets up the Express app, configures middleware, and starts the server.package.json
: Contains metadata about your project, such as dependencies, scripts, and project information.
app.js
: Sets up the Express application and uses routes defined inroutes/
to handle incoming requests.routes/
: Maps incoming requests to the appropriate controller functions.controllers/
: Contains the logic for handling requests, performing necessary actions using the models, and sending responses back to the client.models/
: Interacts with the database to fetch, insert, update, or delete data as requested by the controllers.middlewares/
: Adds additional processing steps for requests, such as verifying that a user is authenticated.config/
: Provides configuration details like database connection settings.views/
andpublic/
: Serve the front-end assets and templates for rendering HTML views (if applicable).
This structure helps keep your code organized and makes it easier to maintain and scale your application as it grows.