Giter Club home page Giter Club logo

rest-api's Introduction

REST API

Unit Tests Packer AMI template validation Build Release

This is a basic skeleton code to create a basic REST API using NodeJS, ExpressJS and Babel.

Note

NOTE: Please note that this is still a work in progress.

Features

  • ES7+
  • NodeJS
  • ExpressJS
  • Winston for logging
  • Basic test scripts
  • Custom error handlers
  • PostgreSQL and Sequelize
  • Bcrypt
  • Custom Amazon Machine Image using Hashicorp Packer
  • Containerization using Docker

Planned features

  • AWS S3, DynamoDB and SNS Topics
  • Email verification of user
  • TypeScript support

Connect to local db

psql postgres

CREATE ROLE me WITH LOGIN PASSWORD 'password';
ALTER ROLE me CREATEDB;

psql -d postgres -U me

CREATE DATABASE test;

\c test

select * from users;

Docker

This is an example repository that will help you dockerize your Node+Express application using Docker.

  1. Create a simple REST API with the following endpoints:
  • / that returns OK status code (200)
  • /health that returns OK status code(200)
  1. Create a simple test that checks the health routes.

  2. Create a new file named Dockerfile at the root of the repository.

  3. Add the following configuration to the above created file:

# which node to use for your container
FROM node:alpine

# the directory where the appication will be copied to in your container
WORKDIR /usr/src/app

# copy the contents of the base(app) directory to the container WORKDIR
COPY . .

# commands to run to install dependencies
RUN npm install

# port that must be exposed to the client where the application is running in the container
EXPOSE 3000

# command to start the application in the container
CMD ["yarn", "start"]
  1. Now, to create an image of your basic REST API:
# -t option to tag the image with a version
# if a version is not provided, the default tag is 'latest'
# the '.' option at the end is to build the current working directory
docker build -t <image-name>:<image-version> .

Example:

docker build -t api:v1 .

To create a docker image using another version of a Dockerfile, use the following command:

docker build -t <image-name>:<image-version> -f <Dockerfile-name> .

Example:

docker build -t api:v2 -f Dockerfile.dev .
  1. To verify your image is build and ready:
# lists all images that docker has built or pulled from docker hub
docker images
  1. To create and run a container:
# -ti is to run the container in an interactive terminal mode
# --name is to name your container
# -p is to expose the port from the container to the local machine where the container is running
docker run -ti --name <container-name> \
-p <local-machine-port>:<container-port> \
<image-name>:<image-version>

Example:

docker run -ti --name rest-api-container -p 3000:1337 api:v1
  1. Test the API endpoints using Postman on http://localhost:3000.

  2. To stop a running container:

docker container stop <container-name>

Example:

docker container stop gpt-container
  1. To restart a stopped container:
docker container start <container-name>

Example:

docker container start gpt-container

To enter the bash terminal within the container:

docker exec -ti <container-name> bash

Example:

docker exec -ti gpt-container bash

Docker bind mount

The -v flag represents the volume in our container, where our application will be uploaded in the Docker Ubuntu image.

docker run --name <container-name> \
-p <local-machine-port>:<container-port> \
-v <local-machine-dir>:<container-WORKDIR-PATH> \
<image-name>:<image-version>

Example:

docker run --name rest-api-container-2 \
-p 3000:1337 \
-v $(pwd):/usr/src/app \
api:v1

NOTE: If you are on a windows computer, $(pwd) will not work. Instead, use %cd%.

Anonymous volume

To leave the node_modules directory untouched in the local machine directory, and to not track it in the docker container, we use anonymous volumes.

docker run --name <container-name> \
-p <local-machine-port>:<container-port> \
-v <local-machine-dir>:<container-WORKDIR-PATH> \
-v <container-WORKDIR/node_modules> \
<image-name>:<image-version>

Example:

docker run --name rest-api-container-3
-p 3000:1337 \
-v $(pwd):/usr/src/app \
-v /usr/src/app/node_modules \
api:v1

Since we do not have the : colon to link this volume in our container to the local machine directory, this is an anonymous volume.

SSH into a container

docker exec -ti <container-name> /bin/sh

Example:

docker exec -ti rest-api-container-3 /bin/sh

Stop and delete containers

  1. Look for the container name:
docker ps
  1. Stop the container using the container name:
docker stop <container-name>

Example:

╰─ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                    NAMES
15fa4c6bbd11   api:v2    "docker-entrypoint.s…"   8 seconds ago   Up 6 seconds   0.0.0.0:3000->1337/tcp   rest-api
docker stop rest-api
  1. To check for stopped containers:
docker ps -a
  1. Make sure that the container is stopped, then proceed to remove that container.
docker ps;
docker rm <container-name>

Example:

docker rm rest-api

Delete docker images

To delete docker images:

  1. Check the name of the image you want to delete:
docker images
  1. Delete the image by it's name:
docker image rm <image-name>:<image-version>

Example:

# check for docker images
docker images
# output
╰─ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
api          v3        4d74db3e5ada   7 hours ago   366MB
api          v2        7bc302778f7c   7 hours ago   366MB
# remove the desired docker image
docker image rm api:v2

Work in progress

Do not build .env into the docker image.

Author

Siddharth Rawat

License

MIT License

rest-api's People

Contributors

semantic-release-bot avatar sydrawat01 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

rest-api's Issues

👨 Fetch user details endpoint

Add controller logic to the user controller to fetch the user data based on the user ID. as well as update the user data based on the user ID.

👨 Create new user endpoint: `POST`

Have a /v1/account endpoint that will create a new user with a JSON request body:

{
  "first_name": "sid",
  "last_name": "rawat",
  "username": "[email protected]",
  "password": "test1234",
}

NOTE: Have basic validations in place and proper HTTP response codes and messages in case of errors.

📝 Update `README.md`

  • Update the README.md to follow community standards.
  • Add a wiki page that lists out steps on how to use the API.

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot 📦🚀

👨 Update user details endpoint

  • Implement controller logic to update the user data.
  • Only an authorized user should be able to update their data.
  • Users should not be able to update another user's data.

🐞 Sync files in docker container

Orchestrate relation between the local repository and the docker image, where changes to the code in the local repository will be reflected in the docker image, without having to restart the server in the docker container.

Also, do not build the docker image with the .env file within the local repository. The .env file must be dynamically created on the fly when creating a docker image. ref

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.