GithubHelp home page GithubHelp logo

abhimanyoo / backend-starter-kit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shyam-chen/express-starter

0.0 1.0 0.0 2.36 MB

:truck: A boilerplate for Node.js, Express, Mongoose, Heroku, mLab, Nodemon, PM2, and Babel. REST / GraphQL API Server | PaaS | SaaS | CaaS | CI/CD | Jest | Supertest | Docker | MongoDB | PostgreSQL | Sequelize | Lodash | RxJS | JWT | Passport | Socket.IO | Redis | CircleCI | Apollo | TensorFlow

License: MIT License

Dockerfile 6.44% JavaScript 92.42% Shell 1.14%
boilerplate nodejs docker pm2

backend-starter-kit's Introduction

Backend Starter Kit

🚚 A boilerplate for Node.js, Express, Mongoose, Heroku, mLab, Nodemon, PM2, and Babel.

Build Status Coverage Status // Dependency Status devDependency Status

🌈 Live Demo

This seed repository provides the following features:

  • ---------- Essentials ----------
  • Application routing with Express.
  • Data query language with GraphQL.
  • Object document mapping with Mongoose.
  • Object relational mapping with Sequelize.
  • Utility functions with Lodash.
  • Reactive extensions with ReactiveX.
  • Authenticate requests with Passport.
  • Real-time bidirectional communication with Socket.
  • In-memory data structure store with Redis.
  • Machine learning with TensorFlow.
  • ---------- Tools ----------
  • Next generation JavaScript with Babel.
  • JavaScript static code analyzer with ESLint.
  • Testing platform with Jest.
  • HTTP testing with Supertest.
  • Test coverage integration with Codecov.
  • Automatically restart application with Nodemon.
  • Keeping application alive with PM2.
  • ---------- Environments ----------
  • Server-side platform with Node.
  • Version control with Git.
  • Code repository with GitHub.
  • Fast and deterministic builds with Yarn.
  • Cloud application hosting with Heroku.
  • Cloud NoSQL database hosting with mLab.
  • Cloud SQL database hosting with ElephantSQL.
  • Cloud memory cache hosting with RedisLabs.
  • Cloud Storageβ€Ž hosting with Cloudinary.
  • Monitoring service with UptimeRobot.
  • Log management service with Papertrail.
  • Performance and security with Cloudflare.
  • Software container with Docker.
  • Continuous integration with CircleCI.

Here are some related seed repositories:

Table of Contents

Getting Started

Follow steps to execute this boilerplate.

  1. Clone this boilerplate
$ git clone --depth 1 https://github.com/Shyam-Chen/Backend-Starter-Kit.git <PROJECT_NAME>
$ cd <PROJECT_NAME>
  1. Install dependencies
$ yarn install
  1. Start a local server
$ yarn start
  1. Compile and bundle code
$ yarn build
  1. Check the code quality
$ yarn lint
  1. Run the unit tests
$ yarn test
  1. Run the end-to-end tests
$ yarn e2e

Dockerization

Dockerize an application.

  1. Build and run the container in the background
$ docker-compose up -d api
  1. Run a command in a running container
$ docker-compose exec api <COMMAND>
  1. Remove the old container before creating the new one
$ docker-compose rm -fs
  1. Restart up the container in the background
$ docker-compose up -d --build api
  1. Push images to Docker Cloud
# .gitignore

  .DS_Store
  node_modules
  dist
  coverage
+ dev.Dockerfile
+ stage.Dockerfile
+ prod.Dockerfile
  *.log
$ docker login
$ docker build -f ./tools/<dev|stage|prod>.Dockerfile -t <IMAGE_NAME>:<IMAGE_TAG> .

# checkout
$ docker images

$ docker tag <IMAGE_NAME>:<IMAGE_TAG> <DOCKER_ID_USER>/<IMAGE_NAME>:<IMAGE_TAG>
$ docker push <DOCKER_ID_USER>/<IMAGE_NAME>:<IMAGE_TAG>

# remove
$ docker rmi <REPOSITORY>:<TAG>
# or
$ docker rmi <IMAGE_ID>
  1. Pull images from Docker Cloud
# circle.yml

+ echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
  docker login -u="<DOCKER_USERNAME>" -p="${HEROKU_TOKEN}" registry.heroku.com
- docker build -f ./tools/<dev|stage|prod>.Dockerfile -t registry.heroku.com/<HEROKU_PROJECT>/web .
+ docker pull <DOCKER_ID_USER>/<IMAGE_NAME>:<IMAGE_TAG>
+ docker tag <IMAGE_NAME>:<IMAGE_TAG> registry.heroku.com/<HEROKU_PROJECT>/web
  docker push registry.heroku.com/<HEROKU_PROJECT>/web

Configuration

Default environments

Set your local environment variables.

// src/env.js

export const NODE_ENV = process.env.NODE_ENV || 'development';

export const HOST = process.env.HOST || '0.0.0.0';
export const PORT = process.env.PORT || 3000;

export const SECRET = process.env.SECRET || 'PUT_YOUR_SECRET_HERE';

export const MONGODB_URI = process.env.MONGODB_URI || '<PUT_YOUR_MONGODB_URI_HERE>';
export const POSTGRES_URL = process.env.POSTGRES_URL || 'PUT_YOUR_POSTGRES_URL_HERE';

export const REDIS_PORT = process.env.REDIS_PORT || '<PUT_YOUR_REDIS_PORT_HERE>';
export const REDIS_HOST = process.env.REDIS_HOST || '<PUT_YOUR_REDIS_HOST_HERE>';

// ...

Deployment environments

Set your deployment environment variables.

# tools/<dev|stage|prod>.Dockerfile

# envs --
ENV SECRET <PUT_YOUR_SECRET_HERE>

ENV MONGODB_URI <PUT_YOUR_MONGODB_URI>
ENV POSTGRES_URL <PUT_YOUR_POSTGRES_URL_HERE>

ENV REDIS_PORT <PUT_YOUR_REDIS_PORT_HERE>
ENV REDIS_HOST <PUT_YOUR_REDIS_HOST_HERE>

# ...
# -- envs

Using Libraries

  1. Example of REST
import { Router } from 'express';

import { List } from './document';

const router = Router();

router.get('/', async (req, res) => {
  const data = await List.find({}).exec();
  res.json(data);
});

export default router;
  1. Example of GraphQL
import gql from 'graphql-tag';

import { List } from './document';

export const listTypeDefs = gql`
  type List {
    _id: ID!
    text: String!
  }

  type Query {
    list: [List]
  }
`;

export const listResolvers = {
  Query: {
    async list(root, { _id, text }) {
      try {
        const data = await List.find({}).exec();
        return data;
      } catch (err) {
        throw err;
      }
    },
  },
};
  1. Example of Document
import mongoose, { Schema } from 'mongoose';

const listSchema = new Schema({
  text: {
    type: String,
    required: true,
  },
});

export const List = mongoose.model('List', listSchema);
  1. Example of Relational
import Sequelize from 'sequelize';

import sequelize from '~/core/sequelize';

export const RelationalList = sequelize.define('List', {
  text: Sequelize.STRING,
});
  1. Example of Lodash
import { of } from 'rxjs';
import { lowerFirst, pad } from 'lodash';

of(lowerFirst('Hello'), pad('World', 5))
  .subscribe(value => console.log(value));
  // hello
  // World
  1. Example of ReactiveX
import { timer, of } from 'rxjs';
import { mapTo, combineAll } from 'rxjs/operators';

timer(2000)
  .pipe(
    mapTo(of('Hello', 'World')),
    combineAll(),
  )
  .subscribe(value => console.log(value));
  // ["Hello"]
  // ["World"]
  1. Example of Socket
import { io } from '~/core/socket';

io.emit('A', { foo: 'bar' });
io.on('B', data => console.log(data));  // { foo: 'baz' }
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
<script>
const socket = io();

socket.on('connect', () => console.log('WS: Accept a connection.'));

socket.on('A', (data) => {
  console.log(data);  // { foo: 'bar' }
  socket.emit('B', { foo: 'baz' });
});
</script>
  1. Example of Redis
import { client } from '~/core/redis';

client.hmset('thing', {
  foo: 'js',
  bar: 'html',
  baz: 'css',
});

client.hgetall('thing', (err, object) => {
  console.log(object);
});

Directory Structure

The structure follows the LIFT Guidelines.

.
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ core  -> core feature module
β”‚   β”œβ”€β”€ <FEATURE>  -> feature modules
β”‚   β”‚   β”œβ”€β”€ __tests__
β”‚   β”‚   β”‚   β”œβ”€β”€ <FEATURE>.e2e-spec.js
β”‚   β”‚   β”‚   └── <FEATURE>.spec.js
β”‚   β”‚   β”œβ”€β”€ _<THING>  -> feature of private things
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   └── <FEATURE>.js
β”‚   β”œβ”€β”€ <GROUP>  -> module group
β”‚   β”‚   └── <FEATURE>  -> feature modules
β”‚   β”‚       β”œβ”€β”€ __tests__
β”‚   β”‚       β”‚   β”œβ”€β”€ <FEATURE>.e2e-spec.js
β”‚   β”‚       β”‚   └── <FEATURE>.spec.js
β”‚   β”‚       β”œβ”€β”€ _<THING>  -> feature of private things
β”‚   β”‚       β”‚   └── ...
β”‚   β”‚       └── <FEATURE>.js
β”‚   β”œβ”€β”€ shared  -> shared feature module
β”‚   β”œβ”€β”€ api.js
β”‚   └── env.js
β”œβ”€β”€ tools
β”‚   └── ...
β”œβ”€β”€ .babelrc
β”œβ”€β”€ .editorconfig
β”œβ”€β”€ .eslintrc
β”œβ”€β”€ .gitignore
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ circle.yml
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ jest.config.js
β”œβ”€β”€ package.json
β”œβ”€β”€ processes.js
└── yarn.lock

backend-starter-kit's People

Contributors

shyam-chen avatar

Watchers

James Cloos avatar

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.