Skip to content

Boilerplate and tooling for authoring data API backends with Node.js and GraphQL

License

Notifications You must be signed in to change notification settings

codingmaven/nodejs-api-starter

 
 

Repository files navigation

Node.js API Starter Kit  

Node.js API Starter Kit is a boilerplate and tooling for authoring data API backends with Node.js, JavaScript (via Babel) and GraphQL. It's meant to be paired with a web and/or mobile application project such as React Starter Kit.

💡 Actively maintained with support from

Features

✓ Cross-platform development on macOS, Windows or Linux inside Docker, single dev dependency
GraphQL boilerplate, everything needed to get started building a GraphQL API endpoint / gateway
PostgreSQL database schema boilerplate and migration tools (see scripts, migrations)
✓ Authentication and authorization via Passport.js (see src/passport.js, src/routes/account.js)
✓ Session and cache management with Redis and DataLoader (see stop using JWT for sessions)
24/7 community support on Gitter + premium support on Skype (book a session)

Directory Layout

.
├── /build/                     # The compiled output (via Babel)
├── /migrations/                # Database schema migrations
├── /scripts/                   # Build automation scripts
├── /src/                       # Node.js application source files
│   ├── /models/                # Data access models, e.g. User.create({ email })
│   ├── /routes/                # Express routes, e.g. /login/facebook
│   ├── /types/                 # GraphQL types with resolve functions
│   │   ├── /Node.js            # Relay's "node" definitions
│   │   ├── /UserType.js        # User account (id, email, etc.)
│   │   ├── /ViewerType.js      # The top-level GraphQL object type
│   │   └── /...                # etc.
│   ├── /app.js                 # Express.js application
│   ├── /db.js                  # Database access and connection pooling (via Knex)
│   ├── /passport.js            # Passport.js authentication strategies
│   ├── /redis.js               # Redis client
│   ├── /schema.js              # GraphQL schema
│   └── /server.js              # Node.js server (entry point)
├── /test/                      # Unit, integration and load tests
├── .env                        # Application settings for the dev environment
├── .env.example                # Available application settings as a reference
├── docker-compose.yml          # Defines Docker services, networks and volumes
├── Dockerfile                  # Commands for building a Docker image for production
├── Dockerfile.dev              # Commands for building a Docker image for development
├── package.json                # The list of project dependencies
└── yarn.lock                   # Fixed versions of all the dependencies

Getting Started

Make sure that you have Docker v1.12.5 or newer installed plus a good text editor or IDE (VS Code, WebStorm or another), clone the repo and launch the app with Docker Compose:

git clone -o nodejs-api-starter -b master --single-branch \
   https://github.com/kriasoft/nodejs-api-starter.git example-api
cd example-api
cp .env.example .env            # Copy environment variables from the template: '.env.example' -> '.env'
docker-compose up               # Launch Docker containers with the Node.js API app running inside

The API server must become available at http://localhost:5000/ (live demo).

Once the docker container named api is started, the Docker engine executes node scripts/run.js command that installs Node.js dependencies, migrates database schema to the latest version, compiles Node.js app from source files (see src) and launches it with "live reload" on port 5000 (see .env).

In order to open a new terminal session from inside the api Docker container run:

docker-compose exec api /bin/sh

From this shell you can run automation scripts such as yarn test, yarn run db:migrate etc. Find the full list of scripts available inside the scripts folder and the package.json file.

Testing

yarn run lint                   # Find problematic patterns in code
yarn run check                  # Check source code for type errors
yarn run test                   # Run unit tests once
yarn run test:watch             # Run unit tests in watch mode

Debugging

In order to run the app with V8 inspector enabled, simply set NODE_DEBUG=true flag in the .env file, restart the app (docker-compose up) and attach your debugger to 127.0.0.1:9229 (see .vscode/launch.json for VS Code as an example).

Keeping Up-to-Date

If you keep the original Git history after clonning this repo, you can always fetch and merge the recent updates back into your project by running:

git checkout master
git fetch nodejs-api-starter
git merge nodejs-api-starter/master
docker-compose up

Deployment

Customize the deployment script found in scripts/publish.js on macOS/Linux or convert it to publish.cmd on Windows. Then whenever you need to deploy your app to a remote server simply run:

/bin/sh scripts/publish.sh      # or, `scripts/publish.cmd` on Windows

Not sure where to deploy your app? DigitalOcean is a great choice in many cases (get $10 credit)

Contributing

Anyone and everyone is welcome to contribute. Start by checking out the list of open issues marked help wanted. However, if you decide to get involved, please take a moment to review the guidelines.

Books and Tutorials

Docker in Action You Don't Know JS JavaScript Ninja Effective JavaScript NodeSchool.io

Related Projects

  • GraphQL.js — The JavaScript reference implementation for GraphQL
  • DataLoader — Batching and caching for GraphQL data access layer
  • React Starter Kit — Isomorphic web app boilerplate (React, Node.js, Babel, Webpack, CSS Modules)
  • React Static Boilerplate — Single-page application (SPA) starter kit (React, Redux, Webpack, Firebase)
  • Membership Database — SQL schema boilerplate for user accounts, profiles, roles, and auth claims

Support

License

Copyright © 2016-present Kriasoft, LLC. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.


Made with ♥ by Konstantin Tarkus (@koistya, blog) and contributors

About

Boilerplate and tooling for authoring data API backends with Node.js and GraphQL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 97.9%
  • Shell 2.1%