Code Monkey home page Code Monkey logo

storefront-backend's Introduction

Udacity Full Stack javascript developer - Storefront Backend

This is the second project of the Udacity Full Stack Javascript Developer Nanodegree program: Storefront Backend.

It consists of an Express server, combined with a Postgres database, that exposes an API for a storefront backend. Users' passwords are stored encrypted in the database, and accessing some endpoints requires a JWT token. All functions to access the Postgres database, and all the API endpoints have associated unit tests.

The starting point for this project is the repository available at: https://github.com/udacity/nd0067-c2-creating-an-api-with-postgresql-and-express-project-starter

Implementation

Key points:

  • Uses Express as web server
  • Uses Postgres for the SQL database
  • Uses bcrypt for password encryption
  • Uses JWT for authorization
  • Uses Jasmine and supertest for unit tests
  • Development done with TypeScript to reduce errors

Deployment

Setting up the Postgres database

In order to run this app you need a properly configured Postgres database. For this, you need a Postgres server running on your machine (see for exemple https://www.postgresql.org/).

Once Postgres is available on your computer, you need to enter psql as admin/root and do the following:

First create the user storefront_user by running in the psql command line:

CREATE USER storefront_user with password '7kJ3DDpqnHe3Qx';

Then create the databases for development and testing

CREATE DATABASE storefront_backend;
CREATE DATABASE storefront_backend_test;

Finally, grant storefront_user access to these databases

GRANT ALL PRIVILEGES ON DATABASE storefront_backend TO storefront_user;
GRANT ALL PRIVILEGES ON DATABASE storefront_backend_test TO storefront_user;

Setting up environment variables

The following environment variables need to be set up:

  • POSTGRES_HOST=127.0.0.1
  • POSTGRES_DB=storefront_backend
  • POSTGRES_DB_TEST=storefront_backend_test
  • POSTGRES_USER=storefront_user
  • POSTGRES_PASSWORD=7kJ3DDpqnHe3Qx
  • ENV=dev
  • BCRYPT_SALT_ROUNDS=10
  • TOKEN_SECRET=LV7erLeKYDY47p (This value can be changed)
  • BCRYPT_PEPPER=ifSgBKoG5HShSW (This value can be changed)

App Installation

The required packages can be installed using

yarn

Dependencies and versions used

The project depends on the following packages:

  • express
  • bcrypt
  • cors
  • db-migrate
  • db-migrate-pg
  • dotenv
  • jsonwebtoken
  • pg
  • typescript

For development, there are additional dependencies

  • jasmine
  • jasmine-spec-reporter
  • jasmine-ts
  • prettier
  • eslint-config-prettier
  • eslint-plugin-prettier
  • nodemon
  • supertest
  • ts-node
  • tsc-watch

See the file package.json for the exact versions used.

Postgres database migrations

To run the migration up in the dev environment, run

db-migrate up

To run the migration up in the test environment, run

db-migrate up -e test

To run the migrations down, substitute downfor up.

Note: all the migrations are in one file, so that tables are created and deleted in an order that is compatible with the use of foreign keys.

Starting the server

In order to start the server that exposes the API, type

yarn run start

Folder structure

The main folders are:

  • src contains all the source files (in Typescript)
    • src/handlers contains the API endpoints (see file REQUIREMENTS.md for details)
    • src/handlers/tests contains the tests for the API endpoints
    • src/models contains functions for accessing the Postgres database (see file REQUIREMENTS.md for details)
    • src/models/tests contains the tests for the functions for accessing the Postgres database
    • src/services contains the function that extracts from the Postgres database the 5 products that appear in most orders (see file REQUIREMENTS.md for details)
    • src/services/tests contains the test for the function that extracts the 5 products that appear in most orders for the API endpoints
    • src/database.ts configures the acces to the Postgres database in javascript based on environment variables
    • src/server.ts contains the code that starts the express server and configures its routes
  • migrations contains the migration to create/delete tables in the database, whether in test or dev
  • build contains the javascript files transpiled from TypeScript
  • speccontains a support file for tests

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.