Skip to content

misael-diaz/twitter-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Twitter API

Academic use

Prerequisites

Create a .env file

Use your preferred text editor to create the .env file:

HTTP_HOST=twitter-api
HTTP_PORT=8080
JWT_SECRET_KEY=jwt_secret_key
JWT_HEADER_KEY=jwt_header_key
DB_CONNECTION_STRING=mongodb://mongodb:27017/details

Note that you may use other hostname and/or port; however, bear in mind that you will need to edit the Dockerfile and the docker commands accordingly. You are strongly encouraged to use other values for the jsonwebtoken secret and header keys, for the ones given here are just examples.

Create a Network with Docker

Create a network with Docker so that DNS lookup works on App startup:

docker network create webnetwork

Start the MongoDB Database

Pull the official image maintained by the Docker community from Docker Hub via (if you have not done so before):

docker pull mongo

Run an instance of the database

docker run --name mongodb --network webnetwork mongo

Note that the name mongodb is the same as the database hostname in the .env file. Feel free to use any other name you deem appropriate for the database.

Dockerize the HTTP App

Use your preferred text editor to create the Dockerfile for building the App Docker Image:

FROM node:16
WORKDIR /usr/src/app
RUN git clone https://github.com/misael-diaz/twitter-api.git /usr/src/app
RUN yarn install
RUN yarn add dotenv
RUN yarn add express
RUN yarn add mongoose
RUN yarn add jsonwebtoken
RUN yarn add bcrypt
RUN yarn add cors
EXPOSE 8080
CMD yarn start

Build the image:

docker image build -t im-twitter-api .

Deploy the Dockerized HTTP App

Deploy the Docker Container of the App on the foreground (useful for testing):

docker container run -it -p 8080:8080 --name twitter-api --network webnetwork --env-file .env im-twitter-api

alternatively, you may execute the App on the background by detaching:

docker container run -it -p 8080:8080 -d --name twitter-api --network webnetwork --env-file .env im-twitter-api

Note that the command requests docker to connect the App to the network webnetwork, that the name of the App twitter-api is the same as the HTTP_HOST in .env, and that the published port 8080 is the same as the HTTP_PORT in .env and the exposed port in the Dockerfile.

Stop the Dockerized HTTP App

To stop the App use:

docker container stop twitter-api

API

Methods
URL GET POST PUT DELETE
/ Displays "App works!" Error Error Error
/api/users/login Error

Authenticate User

{

username,

password

}

public

Error Error
/api/users/signup Error

Create User

{

firstName,

lastName,

email,

username,

password

}

public

Error Error
/api/users/list

private

Authentication

Authorization

Error Error Error

Useful References:

About

REST API Development Exercise

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published