Blog API made with typescript, express & PostgreSQL with all the basic features and more !
- Nodejs
- Expressjs
- Typescript
- PostgreSQL
- Redis
git clone https://github.com/leoantony72/blog_api
npm install
You have to install Postgres Yourself🙃
Start Postgres server❗
sudo service postgresql start
After installing get into postgres CLI
CREATE DATABASE blog; //This will create the database
Next we have to setup TABLES❗
\C blog //this will take you inside blog db
Copy tables From blogapi.sql file(src->models->blog_api.sql)❗
\i FILE PATH
OR YOU CAN COPY/PASTE FROM blogapi.sql
CREATE TABLE users(
userid VARCHAR(11) NOT NULL PRIMARY KEY,
username VARCHAR(50) NOT NULL,
passwordHash text NOT NULL,
user_role VARCHAR(25) NOT NULL,
sessionid text ,
registeredAt TIMESTAMP NOT NULL
);
CREATE INDEX idx_userid ON users(userid);
CREATE TABLE authors(
id VARCHAR(11) NOT NULL PRIMARY KEY,
username VARCHAR(50),
profile_image BYTEA NULL
);
CREATE TABLE post(
post_id VARCHAR(11) NOT NULL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
meta_title VARCHAR(100) NULL,
slug VARCHAR(100) NOT NULL UNIQUE,
summary VARCHAR(100) NULL,
content TEXT NULL DEFAULT NULL,
published VARCHAR(20) NULL,
publishedAt TIMESTAMP NOT NULL,
author_id VARCHAR(11) NULL REFERENCES authors(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE savedpost(
id VARCHAR(11) NOT NULL PRIMARY KEY,
userid VARCHAR(11) NOT NULL REFERENCES users(userid)
ON DELETE CASCADE
ON UPDATE CASCADE,
post_id VARCHAR(11) NOT NULL REFERENCES post(post_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE category(
id VARCHAR(11) NOT NULL PRIMARY KEY,
title VARCHAR(75) NOT NULL,
meta_title VARCHAR(100) NULL DEFAULT NULL,
slug VARCHAR(100) NOT NULL
);
CREATE INDEX idx_category_parent on category(id);
CREATE TABLE post_category(
post_id VARCHAR(11) NOT NULL REFERENCES post(post_id)
ON DELETE CASCADE
ON UPDATE CASCADE ,
category_id VARCHAR(11) NOT NULL REFERENCES category(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE INDEX idx_post_post ON post_category(post_id ASC);
CREATE INDEX idx_post_category ON post_category(category_id ASC);
WITH THIS DB SETUP IS FINISHED😸
These are the env variabes of database❗
PORT = 3000
DB_USER = test
DB_PASSWORD = password
DB_HOST = localhost
DB_PORT = 5432
DB_DATABASE = blog
SESSION_SECRET = secret for session //use a strong one
SESSION_MAXAGE = 3600000 * 60 * 10
GMAIL_USER = example
GMAIL_PASS = password
Create images Folder in Root dir, Inside Images Folder create post_banner Folder❗
npm run dev -L //RUNNNING IN LOCALHOST:3000
The Server is now Running✔
GET POSTS
http://localhost:3000/api/posts //get all posts
GET POST BY ID
http://localhost:3000/api/post/:id //get post by ID
GET POST BY CATEGORY
http://localhost:3000/api/posts/category/:category //get post by category
GET POST BY AUTHOR
http://localhost:3000/api/posts/author/leo //get post by author name
-
Register User🙍♀️
http://localhost:3000/api/auth/register
{
"username": "example",
"email": "example@gmail.com",
"password": "password183$",
"confirmpassword": "password183$"//password are hashed before storing in db
}
- Login User
http://localhost:3000/api/auth/login //Login users
{
"username":"Leoantony72",
"password":"test1234"
}
- Logout Users
http://localhost:3000/api/logout
- Forgot Password
http://localhost:3000/api/auth/forgotpassword
{
"success": "Email Sent"
}
Sends 📧 to provided user, Also checks if user exist in db
USER AND ADMIN
Users cannot create posts,updatepost etc... , Only Admin users can Create post & modify post
By default role is USER ,After registering a user you need go to db to change Role to ADMIN ❗❗
⭕I RECOMMEND USING POSTBIRD (GUI) It is simple to use
USE POSTMAN FOR TESTING❗
- New post (post req)
http://localhost:3000/api/admin/upload //create new post
WE ALSO HAVE TO UPLOAD IMAGE SO CHOOSE *FORM DATA* IN POSTMAN ❗❗
{
"post_id": "hapefdmrktf" //IF SUCCESS IT WILL RETURN A POSTID
}
- Update Post (PUT req)
http://localhost:3000/api/admin/post/hapefdmrktf //update post
"post updated" //if success you will get this
- Delete Post (DELETE req)
http://localhost:3000/api/admin/post/hapefdmrktf //Delete post by post ID
"post deleted" //if success you will get this
Search post by title/meta title
http://localhost:3000/api/search/attack on titan //Search post by post ID
{
"post_id": "itlfosfntlj",
"title": "cool boi",
"meta_title": "attack on titan\r\n",
"slug": "testfgggfgr",
"summary": "res",
"content": "rser",
"published": "published",
"publishedat": "2021-07-19T06:02:55.380Z",
"author_id": "1",
"image": "1626674575348.jpeg"
}
you need to login to use this feature
- Add saved post (POST req)
http://localhost:3000/api/savedpost/itlfosfntlj //save post
{
"success": "Post saved" // if success you will get this
}
- Delete saved post (DELETE req)
http://localhost:3000/api/savedpost/:id //delete saved post
{
"success": "Post deleted" // if success you will get this
}
- Get saved post (GET req)
http://localhost:3000/api/savedpost //Get saved post
//you will get the post if success
[
{
"post_id": "7383e359ddb",
"title": "cool boi",
"meta_title": "leo",
"slug": "nofsea",
"summary": "testsef",
"content": "test",
"published": "published",
"publishedat": "2021-08-04T22:34:30.985Z",
"author_id": "1",
"image": "1628116470935.jpeg"
}
]
- Added Forgot Password
- included session & cookies
- Caching for Posts(/posts,/post/:id)
- Email verification,if not verified you cannot login
- You can only upload .png/.jpg/.jpeg file below 5mb ❗
- Passwords and tokens are hashed & stored in database
- Added input Validation in Login/register/forgotpassword
- Dependencies
Any future updates and additional info will be added