Skip to content

A solid architecture for designing scalable RESTful apis, with auto-reload, validation, async/await support, dependency injection, clean separation of business logic from transport and testable code without mocking http requests and database call.

License

Notifications You must be signed in to change notification settings

alextanhongpin/node-rest

Repository files navigation

JavaScript Style Guide

NodeJS with Babel

Read more about Babel from here.

Clone

git clone https://github.com/alextanhongpin/node-rest.git
git remote rm origin
git remote add origin <your-git-path>

Onion Architecture

Onion Architecture

The architecture is based on the principle of Dependency Inversion. There are layers wrapping around each circle, forming the famous onion. Between the layers of the Onion, there is a strong dependency rule: outer layers can depend on lower layers, but no code in the lower layer can depend directly on any code in the outer layer.

Note that the database is not center, it is external. The moment you grasped this concept, you have grasped the onion architecture.

.babelrc

Here you can specify the version of nodejs that you want the code to compile to. Since we are using AWS Lambda, we want the code to compile to nodejs version 6.10 (7 November 2017). If you are not bounded by this limitation, always use the current version:

{
  "presets": [
    ["env", {
      "targets": {
        "node": "current"
      }
    }]
  ]
}

Installation

First, you have to install Yarn. Then:

# This will install all dependencies from package.json
$ yarn install

# We use foreman to load the environment variables from `.env` file.
# This is important to prevent accidental commit of sensitive data to github
$ yarn global add foreman

Add/Remove packages

$ yarn add <PACKAGE_NAME>
$ yarn add --dev <PACKAGE_NAME>
$ yarn remove <PACKAGE_NAME>

Environment

For development, store all the environment variable in the .env file. This will be included in .gitignore so that it will not be commited to github. Make sure you create the .env file or the service will not run.

The .env should contain the following:

DB_USER=user
DB_PASS=123456
DB_NAME=testdb
DB_HOST=localhost

FOOD_SERVICE=true

Stop MySQL from your local

Any running MySQL will prevent the app from connecting to the docker container.

If you don't stop the MySQL, the following error might appear:

$ error 1044 (42000): access denied for user

Starting the docker image with MySQL DB

$ docker-compose up -d

Start

$ nf start

Test

You can use any reporters that are supported by istanbul: clover, cobertura, html, json-summary, json, lcov, lcovonly, none, teamcity, text-lcov, text-summary, text.

$ yarn test

Report

$ yarn cover

Build

$ yarn build

Accessing Docker MySQL

$ docker exec -it $(docker ps -f name=database -q) mysql -u user -p
Enter password: 123456

Create a Table

CREATE TABLE food (
    id int NOT NULL,
    name varchar(255) NOT NULL,
    PRIMARY KEY (ID)
);

Profiling

Build a production release first:

$ yarn build

Enable profiling:

NODE_ENV=production node --prof app.js

Put some load on the server using ab (Apache Bench):

$ ab -k -c 20 -n 250 "http://localhost:5000/foods"

Process:

$ node --prof-process isolate-0x103800000-v8.log > processed.txt

About

A solid architecture for designing scalable RESTful apis, with auto-reload, validation, async/await support, dependency injection, clean separation of business logic from transport and testable code without mocking http requests and database call.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published