Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
/ TODO-API-nodejs Public archive

An API for TODO notes built with Node.js, Express, MongoDB and JWT

Notifications You must be signed in to change notification settings

fayaz07/TODO-API-nodejs

Repository files navigation


Todo app API with Node.js, Express.js and Mongodb

Contents

  1. Install and configure Node.js
  2. Mongodb Installation and Configuration
  3. Setup and Run the Project

Installation of Node.js

First thing we need to do is to install nodejs, you can find the installation steps and archives from the official website here. It is recommended to use the LTS version of node to avoid any kind of interruptions.

Install specific version using CURL

  1. Install curl
sudo apt update
sudo apt upgrade
sudo apt install curl
  1. Get nodejs PPA Switch to root directory
cd ~
curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh
  1. Run the script under sudo:
sudo bash nodesource_setup.sh
  1. Install nodejs
sudo apt install nodejs
  1. In order for some npm packages to work (those that require compiling code from source, for example), you will need to install the build-essential package:
sudo apt install build-essential

Install Node.js using executable or installer package

  1. Get the installation package or archive from the official website here

  2. If you have downloaded the ubuntu-archive, then you might set the path. Open up your terminal and edit .bashrc file and add the below line at the end of the file.

gedit .bashrc

Note: .bashrc will be located at your home directory

  1. The above command will open up text-editor with .bashrc file, add the following line by replacing the path with path of your nodejs installation directory.
export PATH=<NODEJS-INSTALLATION-PATH>/bin:$PATH

Ex:

export PATH=/usr/local/node-v12.16.2-linux-x64/bin:$PATH

Mongodb installation and configuration

In case you face any issues, refer official docs

Installing mongodb v4.2

a. Import the public key used by the package management system.

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

b. Create a list file for MongoDB

echo  "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

c. Reload local package database.

sudo apt-get update

d. Install the MongoDB packages

sudo apt-get install -y mongodb-org

e. Optional. Although you can specify any available version of MongoDB

echo  "mongodb-org hold" | sudo dpkg --set-selections
echo  "mongodb-org-server hold" | sudo dpkg --set-selections
echo  "mongodb-org-shell hold" | sudo dpkg --set-selections
echo  "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo  "mongodb-org-tools hold" | sudo dpkg --set-selections

Configure mongodb

a. Create a directory to store data

sudo mkdir /data
sudo mkdir /data/db

b. Grant required permissions

sudo chown -R `id -un` /data/db

Start mongodb services

# start mongodb service
sudo service mongod start
# Check status of the service
sudo service mongod status

Initialize MongoDB

sudo mongod

The above command will start a MongoDB instance running on your local machine. I will pick a port to run the database, possibly it will be 27017, so your db will be hosted at

mongodb://localhost:27017/

MongoDB shell

Here you can execute your db queries. Initialize the shell by following command

mongo

Setup and run the project

  1. Install the required dependencies by the following command
npm install
  1. Setup public & private keys for Access and Refresh tokens Open your terminal and type the below commands to create secure private key and extracting public key from the private key.

Creating private key for access token

openssl genrsa -out private.pem 2048

Expected output:

Generating RSA private key, 2048 bit long modulus (2 primes)
....................................................+++++
.+++++

Extracting public key for access token

openssl rsa -in private.pem -outform PEM -pubout -out public.pem

Expected output:

writing RSA key

Creating private key for refresh token

openssl genrsa -out privater.pem 2048

Expected output:

Generating RSA private key, 2048 bit long modulus (2 primes)
....................................................+++++
.+++++

Extracting public key for refresh token

openssl rsa -in privater.pem -outform PEM -pubout -out publicr.pem

Expected output:

writing RSA key

and place these 4 files inside keys directory in root of the project

For more info on openssl, click here

  1. Setup environment variables Rename the .env.example as .env and fill up your details there.

SendGrid

Create an account at SendGrid SendGrid. Create a new API Key here Verify a sender email and use that email in the .env file, to verify click here

  1. Place your application's Database credentials inside the .env file, then inside the config.js file, setup db-names just like todo-test, todo-dev, todo-prod.

Refer below config as example:

const prod = {
    name: 'prod', // name of the environment
    app: {
        port: process.env.PORT || 9000 // this will be the port where app will listen
    },
    db: {
        name: 'todo', // db name
        host: process.env.DB_HOST, // db host
        port: process.env.DB_PORT, // db port, in case you are running in localhost, default port will be: 27017 
        username: process.env.DB_USERNAME, // db username
        password: process.env.DB_PASSWORD // db password
    }
};
  1. Run the project with nodemon
npm run dev

or Run as normal project

npm start