A simple Express server that returns clients and sales of widgets. There's a client app for it which has its own GitHub repo: widget-sales-react.
Contents:
Install Yarn via Homebrew on Mac:
brew update
brew install yarn 1.3.2For other operating systems see the Yarn installation docs.
Clone the repo and run yarn install:
git clone git@github.com:albertkawmi/widget-sales-api.git
cd widget-sales-api
yarn installNOTE: Yarn creates a yarn.lock file, locking dependency versions so that installs run consistently across machines. At project start, latest stable version of Yarn was 1.3.2. If you have any problems installing or running dependencies, please try this version.
Run the server locally:
yarn dev
This will listen on the default port 80 (which is probably undesirable). You can configure this in the .env file in root project directory:
echo PORT=4000 > .env.env is .gitignored so it will not be committed to source control. This means you'll need to add it on each machine you work on.
After yarn dev Nodemon will listen for file changes and restart the local server automatically.
The --inspect flag is used to enable debugging. You can use Chrome Dev tools for this:
- Start the dev server with
yarn dev - Navigate to chrome://inspect
- Click the 'inspect' link for your server under 'Remote Target'
A new window will open with the Chrome debugger.
yarn dev uses Nodemon, debugging and sets the environment variable NODE_ENV=development. If you wish to test the server locally as if it were in production use:
yarn startJest is used as a test runner and tests are written using the Supertest library. Supertest will run the app on port 4242 for testing (this is configurable in package.json).
The following commands are available:
yarn testruns tests onceyarn test:watchruns tests in watch mode for TDDyarn test:coverageruns tests once, with coverage report
yarn eslint
ESLint is used for linting and can be configured in the .eslintrc file. Run yarn eslint to check from the command line (or use an ESLint plugin for your code editor).
Zeit Now.sh is used for cloud infrastructure and hosting. All Now deployments in their free plan will be given a subdomain with a hash under now.sh (see example link below). These URLs can be used as a staging step after local development.
For production, a friendly alias (without the hash) can be redirected to the new instance URL. This redirect means zero downtime. It also means deployments can be reverted by simply reassigning the alias to a previous instance.
yarn deploy:staging
This will run ESLint, all tests and then deploy to a URL like https://widget-sales-api-xxxxx.now.sh
yarn deploy:productionThis will run ESLint, all tests and then deploy to the production alias https://widget-sales-api.now.sh
The alias widget-sales-api is connected to my credentials. You can modify this to anything you like in package.json and Now will allow you to deploy it (you may need to confirm your email address if it's your first time using Now, then it will store credentials on your machine.)
Since the deployment is simply an alias change, it is easy to revert:
- Run
yarn now lsto see a list of running instances - Copy the URL for the instance you wish to revert to
- Run
yarn now alias the-new-instance-xxxxx.now.sh widget-sales-api
NOTE: if you are on the free plan of now.sh you can only have a few instances running concurrently. To remove instances, run the yarn now ls command and then use yarn now rm the-instance-to-remove.now.sh to destroy old instances.