Server - wbobeirne/nycda-ecommerce-server GitHub Wiki

Table of Contents


Folder Layout

.server/
├── index.js   # Server entry point
├── middleware # Reusable middleware
├── models     # Sequelize data models
│
├── routes        # Where all routing happens
│   ├── admin.js  ### Admin pages, render using views
│   ├── api.js    ### Endpoints that deal only in JSON
│   └── react.js  ### Handle rendering the React template, dev server
│
├── util                 # Shared utility functions
│   ├── sequelize.js     ### Sequelize instance w/ config
│   └── uploadToImgur.js ### Imgur uploading relegated to this nasty file
│
└── views            # Admin views only
    ├── pages        ### Pages that are included by template.ejs
    └── template.ejs ### Shared template for admin pages


Models

Models are made using the Sequelize ORM.

Product

Schema

name type constraints description
id integer primary key, auto increment Auto incrementing numeric unique ID
name string(128) not null Display name of product
originalImages json not null Array of source images for thumbnails
images json N/A Array of image objects that have sized thumbnails
price float not null, min 0 Price of the product
rating integer min 1, max 10 Rating from 1 to 10, whole numbers only
description text N/A Arbitrary length description of product
category string(128) N/A Category to group product to
specs json N/A Array of { key, value } strings that describe product specs

Associations

  • Many to Many with Order through order_products

Order

Schema

name type constraints description
id integer primary key, auto increment Auto incrementing numeric unique ID
name string(256) not null Name of person shipping to
address string(256) not null Shipping street address
address2 string(256) N/A Secondary address (apt number, etc.)
city string(256) not null Shipping address city
state string(2) not null State code (NY, not New York)
zipcode string(5) not null 5 number zipcode

|

Associations

  • Many to Many with Products through order_products


Routes

Admin

A typical Express Router, all routes are under /admin. Most pages just do simple DB operations, and render a template from views/pages/*.ejs into views/template.ejs. Some less than great styling and structure going on here, to avoid having to muddle src with admin code.

API

A typical Express Router, all routes are under /api. All routes accept query params (GET, DELETE) or JSON post bodies (POST, PUT) and return JSON. More details are available over at the API docs.

React

The odd one out, exports a function that takes in app as it has to do some application-level configuration. Handles replacing webpack-dev-server in dev mode, and the production express server that just serves up webpack-built files and the admin / api routes.



Imgur

In order to avoid having everyone setup S3, I'm running all image hosting through Imgur's api. This comes with some caveats:

  • Their thumbnails don't do transparency, and add black backgrounds. Therefore, we are manually thumbnailing and uploading each one.
  • They have a rate limit of ~50 uploads per hour, per IP. This makes bulk importing of products or heavy admin tasks a pain in the butt.
  • This code is pretty raunchy, since I was in a pinch.
⚠️ **GitHub.com Fallback** ⚠️